package se252.jan15.calvinandhobbes.project0;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.nio.charset.Charset;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* Echo web service exposed at "echo" path relative to base URI
*
* @author Yogesh Simmhan
* @version 1.0
* @see <a href="http://www.serc.iisc.ernet.in/~simmhan/SE252/">IISc SERC 'SE252:Intro to Cloud Computing' Course Webpage</a>
*
* (c) Yogesh Simmhan, 2015
* This work is licensed under a Attribution 4.0 International (CC BY 4.0).
* http://creativecommons.org/licenses/by/4.0/
*/
@Path("directionService")
public class IIScCampusMapDirectionService {
/**
* Method handling HTTP GET requests. The request expects a "msg" input parameter with value of type string.
* The returned object will be sent to the client as JSON media type.
*
* @return JSON form of EchoMessage will be returned as response.
*/
private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}
private static String getGeoJSON(String url) {
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.iisc.ernet.in", 3128));
InputStream is;
try {
is = new URL(url).openConnection(proxy).getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
is.close();
return jsonText;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/*private static JSONObject getTimezone(String location, long timestamp) {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(timezoneLink);
System.out.println("Trying timezone");
String timezone = target.path("json")
.queryParam("location", location)
.queryParam("timestamp", String.valueOf(timestamp))
.queryParam("key", apiGoogle)
.request().get(String.class);
System.out.println("timezone done!");
return new JSONObject(timezone);
}*/
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response IIScCampusMapGet(@DefaultValue("0") @QueryParam("fLat") String fLat,
@DefaultValue("0") @QueryParam("fLon") String fLon,
@DefaultValue("0") @QueryParam("tLat") String tLat,
@DefaultValue("0") @QueryParam("tLon") String tLon) {
System.out.println("called directions service");
String url = "http://www.yournavigation.org/api/1.0/gosmore.php?format=geojson&flat="
+ fLat + "&flon=" + fLon + "&tlat=" + tLat + "&tlon=" + tLon
+ "&v=foot&fast=1&layer=mapnik";
String resp = null;
resp = getGeoJSON(url);
return Response.ok(resp, MediaType.TEXT_PLAIN).header("Access-Control-Allow-Origin", "*").build();
}
}