com.nazara.proxy.api.Endpoint.java Source code

Java tutorial

Introduction

Here is the source code for com.nazara.proxy.api.Endpoint.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nazara.proxy.api;

import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;
import com.nazara.proxy.dto.IPInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/")
public class Endpoint {

    static Logger logger = LoggerFactory.getLogger(Endpoint.class);

    @RequestMapping(path = "/status", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<Object> proxyRequest(@RequestParam(value = "msisdn", required = false) String msisdn) {
        logger.info("status request arrived");
        try {
            HttpResponse<IPInfo> ipInfoResponse = Unirest.get("http://ipinfo.io/")
                    .header("accept", "application/json").asObject(IPInfo.class);
            if (ipInfoResponse.getStatus() == 200) {
                return new ResponseEntity<Object>(ipInfoResponse.getBody(), HttpStatus.OK);
            }
        } catch (Exception exception) {
            logger.error("Error while processing proxy request {}", exception);
            return new ResponseEntity<Object>("NOK|ERR_CODE", HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity<Object>("NOK|ERR_CODE", HttpStatus.NO_CONTENT);
    }
}