Subversion Repositories SmartDukaan

Rev

Rev 11964 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.serving.services;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import in.shop2020.serving.utils.Utils;
import in.shop2020.serving.utils.SnippetType;

public class ContentServingService {
        private static Logger log = Logger.getLogger(ContentServingService.class);
        
        public static String getSnippet(SnippetType type, String entityId, long sourceId){
                String filename;
                if(sourceId == -1){
                        filename = Utils.EXPORT_ENTITIES_PATH + entityId + File.separator + type.getFilename();
                }else{
                        filename = Utils.EXPORT_ENTITIES_PATH + entityId + File.separator + sourceId + File.separator + type.getFilename();
                }
                String htmlString = "";
                File file = new File(filename);
                try {
                        htmlString =  FileUtils.readFileToString(file);
                } catch (IOException e) {
                        log.warn("Snippet of type " + type + " for entity " + entityId + " not found. ");
                }
                return htmlString;
        }
        
        public static String getJSON(String entityId){
                String endPoint = "http://localhost:8080/mobileapi/entity/" + entityId + "/getShortContent";
                return sendGetRequest(endPoint, null);
        }
        
        /**
         * 
         * @param endpoint
         * @param requestParameters
         * @return
         */
         public static String sendGetRequest(String endpoint, String requestParameters)
     {
             String result = null;
             try
             {
                     String urlStr = endpoint;
                     if (requestParameters != null && requestParameters.length () > 0){
                             urlStr += "?" + requestParameters;
                     }
                     URL url = new URL(urlStr);
                     URLConnection conn = url.openConnection ();

                     // Get the response
                     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                     StringBuffer sb = new StringBuffer();
                     String line;
                     while ((line = rd.readLine()) != null)
                     {
                             sb.append(line);
                     }
                     rd.close();
                     result = sb.toString();
             } catch (Exception e){
                     log.info("Could not get results from api locally");
             }
             return result;
     }
}