Skip to content

Commit 8c06885

Browse files
committed
Worked around Safari's limitation about file download
1 parent b01fefc commit 8c06885

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

EDI-T/.classpath

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@
1212
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
1313
</attributes>
1414
</classpathentry>
15-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 7 [1.7.0_17]"/>
15+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.launching.macosx.MacOSXType/Java SE 7 [1.7.0_51]"/>
1616
<classpathentry kind="output" path="target/classes"/>
1717
</classpath>

EDI-T/src/it/cnr/irea/ediT/rest/RestBase.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import javax.xml.xpath.XPathFactory;
4040

4141
import org.springframework.beans.factory.annotation.Autowired;
42+
import org.springframework.http.HttpEntity;
43+
import org.springframework.http.HttpHeaders;
4244
import org.springframework.http.HttpStatus;
4345
import org.springframework.http.MediaType;
4446
import org.springframework.http.ResponseEntity;
@@ -157,8 +159,6 @@ public ResponseEntity<PostMetadataResponse> postMetadataNG(HttpServletRequest re
157159
// return Response.ok(xml).build();
158160
Document xmlDoc = document.getOutput();
159161

160-
service.saveMetadata(document, xml, elementList, service.isTestServer(req));
161-
162162
document.saveTo("/tmp/last_md.xml");
163163

164164
try {
@@ -174,6 +174,7 @@ public ResponseEntity<PostMetadataResponse> postMetadataNG(HttpServletRequest re
174174
response.setMessages(document.getMessages());
175175

176176
if ( elementList.getXsltChain() == null ) {
177+
service.saveMetadata(document, xml, elementList, service.isTestServer(req));
177178
} else {
178179
log.info("xsltChain is correctly defined in EDIML");
179180
String xmlTemp = new String(document.xmlUTF8String(xmlDoc), "utf-8");
@@ -187,9 +188,10 @@ public ResponseEntity<PostMetadataResponse> postMetadataNG(HttpServletRequest re
187188
xmlTemp = new String(result, "utf-8");
188189
}
189190
}
191+
document.setOutput(service.loadXMLFromString(xmlTemp));
192+
service.saveMetadata(document, xml, elementList, service.isTestServer(req));
190193
response.setGeneratedXml(xmlTemp);
191-
192-
System.out.println("Морски");
194+
193195
return new ResponseEntity<PostMetadataResponse>(response, HttpStatus.OK);
194196
}
195197
} catch (Exception e) {
@@ -210,6 +212,42 @@ public ResponseEntity<PostMetadataResponse> optionsPostMetadata(String xml) {
210212
}
211213

212214

215+
@RequestMapping(method = RequestMethod.GET, value = "/rest/xml/{id}.{suffix}", produces = MediaType.APPLICATION_XML_VALUE)
216+
@ResponseBody
217+
public HttpEntity<byte[]> getXml(@PathVariable int id, @PathVariable String suffix) {
218+
log.info("getXml " + id + ", " + suffix);
219+
Metadata md = service.getMetadata(id);
220+
221+
HttpHeaders header = new HttpHeaders();
222+
// header.setContentType(new MediaType("application", "pdf"));
223+
header.set("Content-Disposition",
224+
"attachment; filename=generated_" + id + ".xml");
225+
// header.setContentLength(documentBody.length);
226+
227+
return new HttpEntity<byte[]>(md.getOutput().getBytes(), header);
228+
}
229+
230+
@RequestMapping(method = RequestMethod.GET, value = "/rest/edimlFile/{id}.{suffix}", produces = MediaType.APPLICATION_XML_VALUE)
231+
@ResponseBody
232+
public HttpEntity<byte[]> getEdimlFile(@PathVariable int id, @PathVariable String suffix) {
233+
log.info("getEdimlFile " + id + ", " + suffix);
234+
Metadata md = service.getMetadata(id);
235+
236+
HttpHeaders header = new HttpHeaders();
237+
// header.setContentType(new MediaType("application", "pdf"));
238+
header.set("Content-Disposition",
239+
"attachment; filename=ediml_" + id + ".xml");
240+
// header.setContentLength(documentBody.length);
241+
Document doc = null;
242+
try {
243+
doc = service.loadXMLFromString(md.getInput());
244+
} catch (Exception e) {
245+
// TODO Auto-generated catch block
246+
e.printStackTrace();
247+
}
248+
return new HttpEntity<byte[]>(doc.toString().getBytes(), header);
249+
}
250+
213251
@RequestMapping(method = RequestMethod.GET, value = "/rest/ediml/{id}", produces = MediaType.APPLICATION_XML_VALUE)
214252
@ResponseBody
215253
public String getEDIMLXml(@PathVariable int id) {

EDI-T/src/it/cnr/irea/ediT/service/BaseService.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
import it.cnr.irea.ediT.model.Setting;
88
import it.cnr.irea.ediT.model.TemplateElementList;
99

10+
import java.io.ByteArrayInputStream;
1011
import java.io.UnsupportedEncodingException;
1112
import java.util.Date;
1213

1314
import javax.persistence.EntityManager;
1415
import javax.servlet.http.HttpServletRequest;
16+
import javax.xml.parsers.DocumentBuilder;
17+
import javax.xml.parsers.DocumentBuilderFactory;
1518

1619
import org.apache.log4j.Logger;
1720
import org.springframework.beans.factory.annotation.Autowired;
@@ -43,6 +46,15 @@ public void doSync() {
4346
}
4447
}
4548

49+
public Document loadXMLFromString(String xml) throws Exception
50+
{
51+
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
52+
53+
factory.setNamespaceAware(true);
54+
DocumentBuilder builder = factory.newDocumentBuilder();
55+
56+
return builder.parse(new ByteArrayInputStream(xml.getBytes()));
57+
}
4658

4759
public String getHostName() {
4860
return getSetting("starterKitURI", null);

0 commit comments

Comments
 (0)