Commit f731fa81 authored by Bc. Edvard Rejthar's avatar Bc. Edvard Rejthar

autentizace

google api
parent b38d7e05
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.aos</groupId>
<artifactId>airline</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Airline</name>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.aos</groupId>
<artifactId>airline</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Airline</name>
<properties>
<jersey.version>2.5.1</jersey.version>
<genson.version>0.98</genson.version>
<eclipselink.version>2.5.2-M1</eclipselink.version>
<derby.version>10.10.1.1</derby.version>
<junit.version>4.8.2</junit.version>
<mockito.version>1.9.5</mockito.version>
<properties>
<jersey.version>2.5.1</jersey.version>
<genson.version>0.98</genson.version>
<eclipselink.version>2.5.2-M1</eclipselink.version>
<derby.version>10.10.1.1</derby.version>
<junit.version>4.8.2</junit.version>
<mockito.version>1.9.5</mockito.version>
<mysqlconnector.version>5.1.6</mysqlconnector.version>
</properties>
</properties>
<dependencies>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
......@@ -40,62 +40,75 @@
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>${genson.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>${genson.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>${eclipselink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>${derby.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>
<version>${jersey.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlconnector.version}</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
<type>jar</type>
</dependency>
</dependencies>
<build>
<finalName>airline</finalName>
</build>
<build>
<finalName>airline</finalName>
</build>
<developers>
<developer>
<name>Edvard Rejthar</name>
<roles>
<role>Team</role>
</roles>
</developer>
</developers>
<developers>
<developer>
<name>Edvard Rejthar</name>
<roles>
<role>Team</role>
</roles>
</developer>
</developers>
</project>
\ No newline at end of file
package cz.aos.api;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.*;
import cz.aos.api.data.Destination;
import cz.aos.persistence.PersistenceException;
import cz.aos.service.DestinationService;
......@@ -9,67 +12,121 @@ import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.net.ssl.HttpsURLConnection;
/**
* @author rejthedv
*/
@Path(value = "destination")
public class DestinationResource {
protected DestinationService service;
public DestinationResource() {
this.service = new DestinationService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
ArrayList<Destination> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Destination getData(@PathParam("id") Long id) {
return this.service.find(id);
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
public Response saveDestination(Destination data) {
try {
this.service.create(data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " created.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Destination data) {
try {
this.service.update(id, data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " updated.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteData(@PathParam("id") Long id) {
try {
this.service.delete(id);
return Response.status(Response.Status.OK).entity("Destination " + id + " deleted.").type(MediaType.APPLICATION_JSON).build();
} catch(PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
protected DestinationService service;
public DestinationResource() {
this.service = new DestinationService();
}
@GET
@Path("/")
@Produces({MediaType.APPLICATION_JSON})
public Response getAll() {
ArrayList<Destination> list = this.service.find();
Response.ResponseBuilder builder = Response.ok(list);
return builder.build();
}
@GET
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Destination getData(@PathParam("id") Long id) {
return this.service.find(id);
}
@POST
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@RolesAllowed({"admin"})
public Response saveDestination(Destination data) {
try {
String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + data.getName() + "&sensor=false";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Czech Technical University - AOS bot - Rejthar Edvard");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader in = new BufferedReader(isr);
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//zpracovat dosly JSON
System.out.println("hulvat");
System.out.println(response.toString());
JsonParser parser = new JsonParser();
System.out.println("Testab");
JsonObject rootObj = parser.parse(response.toString()).getAsJsonObject();
System.out.println(rootObj);
JsonObject locObj = rootObj.getAsJsonArray("results").get(0).getAsJsonObject().getAsJsonObject("geometry").getAsJsonObject("location");
String status = rootObj.get("status").getAsString();
String lat = locObj.get("lat").getAsString();
String lng = locObj.get("lng").getAsString();
data.setLatitude(Double.parseDouble(lat));
data.setLongitude(Double.parseDouble(lng));
this.service.create(data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " created.").type(MediaType.APPLICATION_JSON).build();
} catch (PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch (MalformedURLException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
} catch (IOException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@PUT
@Path("{id}")
@Produces({MediaType.APPLICATION_JSON})
public Response updateData(@PathParam("id") Long id, Destination data) {
try {
this.service.update(id, data);
return Response.status(Response.Status.OK).entity("Destination " + data.getId() + " updated.").type(MediaType.APPLICATION_JSON).build();
} catch (PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
@DELETE
@Path("{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response deleteData(@PathParam("id") Long id) {
try {
this.service.delete(id);
return Response.status(Response.Status.OK).entity("Destination " + id + " deleted.").type(MediaType.APPLICATION_JSON).build();
} catch (PersistenceException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).type(MediaType.APPLICATION_JSON).build();
}
}
}
......@@ -9,7 +9,7 @@ import java.util.ArrayList;
import java.util.List;
/**
* @author rejthedv27.10.14.
* @author rejthedv 27.10.14.
*/
public class DestinationService {
......
......@@ -7,4 +7,7 @@
<description>Keep a copy of the generated servlet class' java code.</description>
</property>
</jsp-config>
</glassfish-web-app>
<security-role-mapping> <role-name>admin</role-name> <principal-name>b</principal-name> </security-role-mapping>
</glassfish-web-app>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
<security-role-mapping>
<role-name>admin</role-name>
<group-name>admin</group-name>
</security-role-mapping>
</sun-web-app>
\ No newline at end of file
......@@ -2,21 +2,62 @@
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html#d4e194 -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<filter>
<filter-name>Rest</filter-name>
<filter-class>com.sun.jersey.spi.container.servlet.ServletContainer</filter-class>
<init-param>
<!-- filter skenující povolené role pro jednotlivé metody -->
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</param-value>
</init-param>
</filter>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Metody DELETE a POST vyžadují přihlášení -->
<security-constraint>
<web-resource-collection>
<web-resource-name>AdminDestination</web-resource-name>
<url-pattern>/source/destination/*</url-pattern>
<http-method>DELETE</http-method>
<http-method>PUT</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/source/*</url-pattern>
</servlet-mapping>
<!-- Konfigurace loginu -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>file</realm-name>
</login-config>
<!-- Security role -->
<security-role>
<role-name>admin</role-name>
</security-role>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>false</param-value>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment