Skip to content

Commit 667f516

Browse files
committed
Fixed so GetTemplate for download Subscription JSON Template file works when executing from jar file also.
1 parent 80234b8 commit 667f516

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/main/java/com.ericsson.ei.frontend/SubscriptionFilesController.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,27 @@
1616
*/
1717
package com.ericsson.ei.frontend;
1818

19+
import java.io.BufferedInputStream;
20+
import java.io.BufferedReader;
1921
import java.io.File;
2022
import java.io.FileInputStream;
2123
import java.io.FileNotFoundException;
2224
import java.io.IOException;
2325
import java.io.InputStream;
26+
import java.io.InputStreamReader;
27+
import java.io.StringWriter;
28+
import java.net.URL;
29+
import java.nio.charset.Charset;
30+
import java.nio.charset.StandardCharsets;
31+
import java.nio.file.Files;
32+
import java.nio.file.Path;
33+
import java.nio.file.Paths;
2434

2535
import javax.servlet.ServletContext;
2636
import javax.servlet.http.HttpServletRequest;
2737
import javax.servlet.http.HttpServletResponse;
2838

39+
import org.omg.IOP.Encoding;
2940
import org.springframework.beans.factory.annotation.Autowired;
3041
import org.springframework.beans.factory.annotation.Value;
3142
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -37,7 +48,9 @@
3748
import org.springframework.web.bind.annotation.RequestMethod;
3849
import org.springframework.web.bind.annotation.ResponseBody;
3950

51+
import com.google.common.io.CharStreams;
4052
import com.sun.glass.ui.Application;
53+
import org.apache.tomcat.util.http.fileupload.IOUtils;
4154

4255
import org.springframework.web.bind.annotation.RequestBody;
4356

@@ -48,29 +61,31 @@ public class SubscriptionFilesController {
4861
@Autowired
4962
private ResourceLoader resourceLoader;
5063

64+
@Autowired
65+
ServletContext servletContext;
66+
5167
@Value("${ei.subscriptionFilePath}") private String subscriptionFilePath;
5268
private static final String APPLICATION_JSON = "application/json";
5369

5470

5571
@RequestMapping(value = "/download/subscriptiontemplate", method = RequestMethod.GET, produces = APPLICATION_JSON)
56-
public @ResponseBody void getSubscriptionJsonTemplate(HttpServletResponse response) throws IOException {
72+
public void getSubscriptionJsonTemplate(HttpServletResponse response) throws IOException {
5773

58-
Resource resource = resourceLoader.getResource("classpath:" + subscriptionFilePath);
59-
File file = resource.getFile();
60-
InputStream in = new FileInputStream(file);
74+
InputStream is = null;
75+
try {
76+
is = getClass().getResourceAsStream("/subscriptionsTemplate.json");
77+
} catch (NullPointerException e) {
78+
System.out.println("ERROR: " + e.getMessage());
79+
}
80+
81+
try {
82+
IOUtils.copy(is, response.getOutputStream());
83+
response.getOutputStream().flush();
84+
} catch (IOException e) {
85+
System.out.println("Error :- " + e.getMessage());
86+
}
6187

62-
response.setContentType(APPLICATION_JSON);
63-
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
64-
response.setHeader("Content-Length", String.valueOf(file.length()));
65-
FileCopyUtils.copy(in, response.getOutputStream());
6688
}
67-
68-
69-
// @RequestMapping(value = "/upload/subscriptions", method = RequestMethod.GET, produces = APPLICATION_JSON)
70-
// public @ResponseBody void validateAndCreateSubscriptions(@RequestBody File subscriptionJsonFile) throws IOException {
71-
//
72-
//
73-
// }
7489

7590

7691
private File getFile(String filePath) throws FileNotFoundException {

0 commit comments

Comments
 (0)