| 3561 |
rajveer |
1 |
package in.shop2020.serving.services;
|
|
|
2 |
|
| 11964 |
amit.gupta |
3 |
import java.io.BufferedReader;
|
| 3561 |
rajveer |
4 |
import java.io.File;
|
|
|
5 |
import java.io.IOException;
|
| 11964 |
amit.gupta |
6 |
import java.io.InputStreamReader;
|
|
|
7 |
import java.net.URL;
|
|
|
8 |
import java.net.URLConnection;
|
|
|
9 |
|
| 3561 |
rajveer |
10 |
import org.apache.commons.io.FileUtils;
|
|
|
11 |
import org.apache.log4j.Logger;
|
|
|
12 |
|
|
|
13 |
import in.shop2020.serving.utils.Utils;
|
|
|
14 |
import in.shop2020.serving.utils.SnippetType;
|
|
|
15 |
|
|
|
16 |
public class ContentServingService {
|
|
|
17 |
private static Logger log = Logger.getLogger(ContentServingService.class);
|
|
|
18 |
|
|
|
19 |
public static String getSnippet(SnippetType type, String entityId, long sourceId){
|
|
|
20 |
String filename;
|
|
|
21 |
if(sourceId == -1){
|
|
|
22 |
filename = Utils.EXPORT_ENTITIES_PATH + entityId + File.separator + type.getFilename();
|
|
|
23 |
}else{
|
|
|
24 |
filename = Utils.EXPORT_ENTITIES_PATH + entityId + File.separator + sourceId + File.separator + type.getFilename();
|
|
|
25 |
}
|
|
|
26 |
String htmlString = "";
|
|
|
27 |
File file = new File(filename);
|
|
|
28 |
try {
|
|
|
29 |
htmlString = FileUtils.readFileToString(file);
|
|
|
30 |
} catch (IOException e) {
|
| 10190 |
amit.gupta |
31 |
log.warn("Snippet of type " + type + " for entity " + entityId + " not found. ");
|
| 3561 |
rajveer |
32 |
}
|
|
|
33 |
return htmlString;
|
|
|
34 |
}
|
| 11964 |
amit.gupta |
35 |
|
|
|
36 |
public static String getJSON(String entityId){
|
|
|
37 |
String endPoint = "http://localhost:8080/mobileapi/items/" + entityId;
|
|
|
38 |
return sendGetRequest(endPoint, null);
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
*
|
|
|
43 |
* @param endpoint
|
|
|
44 |
* @param requestParameters
|
|
|
45 |
* @return
|
|
|
46 |
*/
|
|
|
47 |
public static String sendGetRequest(String endpoint, String requestParameters)
|
|
|
48 |
{
|
|
|
49 |
String result = null;
|
|
|
50 |
try
|
|
|
51 |
{
|
|
|
52 |
String urlStr = endpoint;
|
|
|
53 |
if (requestParameters != null && requestParameters.length () > 0){
|
|
|
54 |
urlStr += "?" + requestParameters;
|
|
|
55 |
}
|
|
|
56 |
URL url = new URL(urlStr);
|
|
|
57 |
URLConnection conn = url.openConnection ();
|
|
|
58 |
|
|
|
59 |
// Get the response
|
|
|
60 |
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
|
|
|
61 |
StringBuffer sb = new StringBuffer();
|
|
|
62 |
String line;
|
|
|
63 |
while ((line = rd.readLine()) != null)
|
|
|
64 |
{
|
|
|
65 |
sb.append(line);
|
|
|
66 |
}
|
|
|
67 |
rd.close();
|
|
|
68 |
result = sb.toString();
|
|
|
69 |
} catch (Exception e){
|
|
|
70 |
log.info("Could not get results from api locally");
|
|
|
71 |
}
|
|
|
72 |
return result;
|
|
|
73 |
}
|
| 3561 |
rajveer |
74 |
}
|