Subversion Repositories SmartDukaan

Rev

Rev 23532 | Rev 25427 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.google.gson.Gson;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.web.res.EntityResponse;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
public class EntityController {

        private static final Logger logger=LogManager.getLogger(EntityController.class);
        
        @Value("${saholic.api.host}")
        private String host;
        
        @Value("${saholic.api.port}")
        private int port;
        
        @Value("${saholic.api.webapp}")
        private String webapp;
        
        @Autowired
        private ResponseSender<?> responseSender;
        
        @RequestMapping(value = ProfitMandiConstants.URL_ENTITY, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        
        @ApiOperation(value = "Get entity content")
        public ResponseEntity<?> getEntity(HttpServletRequest request, @PathVariable(value="entityId") String entityId)throws ProfitMandiBusinessException{
                logger.info("Entity Id "+entityId);
                RestClient rc = new RestClient();
                Map<String, String> params = new HashMap<String, String>();
                String response = null;
                String uri = webapp+"/entity/"+entityId;
                EntityResponse entityResponse=null;
                try {
                        response = rc.get(SchemeType.HTTP, host , port, uri, params);
                } catch (HttpHostConnectException e) {
                        throw new ProfitMandiBusinessException("", "", "Could not connect to host");
                }
                
                Gson gson = new Gson();
                entityResponse = gson.fromJson(response, EntityResponse.class);
                return responseSender.ok(entityResponse);
        }


        

}