package solysombra.server;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.restlet.resource.ClientResource;
import solysombra.client.MashupService;
import solysombra.shared.domain.wwo.Forecast;
import solysombra.shared.places.Places;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.HttpMethod;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.util.NameValuePair;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
/**
* The server side implementation of the RPC service.
*/
@SuppressWarnings("serial")
public class MashupServiceImpl extends RemoteServiceServlet implements
MashupService {
// TODO: Ejercicio 2.a: Obtener una api key y use esta constante para
// guardarla
private static final String WWO_API_KEY = "xhfhyuvuvegtfs2juvcstyjr";
private static final String PLACES_API_KEY = "AIzaSyCXnJLxDyzGY_v7uV8BenDHP9v-_lY7brI";
public Forecast getWWOForecast(double lat, double lon) {
// http://api.worldweatheronline.com/free/v1/weather.ashx?key=xhfhyuvuvegtfs2juvcstyjr&q=48.85,2.35&fx=no&format=json
String uri = "http://api.worldweatheronline.com/free/v1/weather.ashx?key="
+ WWO_API_KEY + "&q=" + lat + "," + lon + "&fx=no&format=json";
System.out.println(uri);
ClientResource cr = new ClientResource(uri);
Forecast res = cr.get(Forecast.class);
return res;
}
public Places getPlaces(Double lat, Double lon, String types, String name) {
String uri = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+ lat + "," + lon +"&radius=5000&types="+types+"&name="+name+"&sensor=false&key="+PLACES_API_KEY;
System.out.println(uri);
ClientResource cr = new ClientResource(uri);
Places places = cr.get(Places.class);
return places;
}
// TODO: Ejercicio 3: Implemente la llamada al servicio de MusicGraph
public String getFormResponse(double lon, double lat, int mes, int dia,
int hora) {
String res = "valorPrueba !!";
final WebClient webClient = new WebClient();
try {
String clave = "nWphymts1a5lgpWHk2yDh41remeWq9W0ndij1MebnpXZrJ2frKietKhkZYeLZmZnZmRpYpx8pXKT3JXXyqa/oaRlpeRlmuN7o62gvg";
WebWindow aux = webClient.getCurrentWindow();
aux.toString();
URL url = new URL(
"http://huellasolar.com/visorweb/client_serv/serv_sunperc.php");
WebRequest req = new WebRequest(url, HttpMethod.POST);
List<NameValuePair> l = new ArrayList<NameValuePair>();
l.add(new NameValuePair("user_key", clave));
// l.add(new NameValuePair("x", String.valueOf((lon))));
l.add(new NameValuePair("x", String.valueOf(lon)));
// l.add(new NameValuePair("y", String.valueOf((lat))));
l.add(new NameValuePair("y", String.valueOf(lat)));
l.add(new NameValuePair("mes", String.valueOf(mes)));
l.add(new NameValuePair("userdia", String.valueOf(dia)));
l.add(new NameValuePair("hora", String.valueOf(hora)));
req.setRequestParameters(l);
HtmlPage pageRes = webClient.getPage(req);
res = pageRes.getBody().asText();
} catch (FailingHttpStatusCodeException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return res;
}
}