Commit 8304918f authored by Piotr Gawron's avatar Piotr Gawron
Browse files

first rest methods

* login and rest token managament implemented
* getting of project info via rest added
parent 67bc0bc4
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
<springframework.version>4.2.0.RELEASE</springframework.version> <springframework.version>4.2.0.RELEASE</springframework.version>
<springframework.security.version>4.0.2.RELEASE</springframework.security.version> <springframework.security.version>4.0.2.RELEASE</springframework.security.version>
<springframework.security.oauth.version>2.0.12.RELEASE</springframework.security.oauth.version>
<springframework.webflow.version>2.4.2.RELEASE</springframework.webflow.version> <springframework.webflow.version>2.4.2.RELEASE</springframework.webflow.version>
<xercesImp.version>2.11.0</xercesImp.version> <xercesImp.version>2.11.0</xercesImp.version>
...@@ -31,7 +34,6 @@ ...@@ -31,7 +34,6 @@
<jersey.version>1.18.1</jersey.version> <jersey.version>1.18.1</jersey.version>
<rs-jax.version>1.1.1</rs-jax.version> <rs-jax.version>1.1.1</rs-jax.version>
<log4j.version>1.2.17</log4j.version> <log4j.version>1.2.17</log4j.version>
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes> <attributes>
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
......
<?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0"> <?xml version="1.0" encoding="UTF-8"?><project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="MapViewer-rest-api"> <wb-module deploy-name="MapViewer-rest-api">
<wb-resource deploy-path="/" source-path="/src/main/java"/> <wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
</wb-module> </wb-module>
</project-modules> </project-modules>
...@@ -20,6 +20,12 @@ ...@@ -20,6 +20,12 @@
<version>1.0</version> <version>1.0</version>
</dependency> </dependency>
<dependency>
<groupId>lcsb.mapviewer</groupId>
<artifactId>service</artifactId>
<version>1.0</version>
</dependency>
<dependency> <dependency>
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId> <artifactId>jsr311-api</artifactId>
...@@ -36,5 +42,12 @@ ...@@ -36,5 +42,12 @@
<artifactId>jersey-servlet</artifactId> <artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version> <version>${jersey.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-faces</artifactId>
<version>${springframework.webflow.version}</version>
</dependency>
</dependencies> </dependencies>
</project> </project>
package lcsb.mapviewer.api;
import org.apache.log4j.Logger;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
public abstract class BaseController {
Logger logger = Logger.getLogger(BaseController.class);
@ExceptionHandler({ Exception.class })
public ResponseEntity<Object> handleException(Exception e, WebRequest request) {
logger.error(e, e);
if (e instanceof SecurityException) {
return new ResponseEntity<Object>("{\"error\" : \"Access denied.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(), HttpStatus.FORBIDDEN);
} else {
return new ResponseEntity<Object>(
"{\"error\" : \"Internal server error.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
package lcsb.mapviewer.api;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import lcsb.mapviewer.model.Project;
/**
* RESTfull API servlet for operations on {@link Project} objects.
*
* @author Piotr Gawron
*
*/
@Path("/project")
public class ProjectApiServlet {
/**
* Returns list of projects.
*
* @return {@link Response} object with list of projects
*/
@GET
@Path("/list")
public Response getMsg() {
String output = "Hello";
return Response.status(Response.Status.OK).entity(output).build();
}
}
package lcsb.mapviewer.api.controller;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.services.interfaces.IUserService;
import lcsb.mapviewer.services.view.AuthenticationToken;
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
Logger logger = Logger.getLogger(UserController.class);
@Autowired
private IUserService userService;
@RequestMapping(value = "/login", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public AuthenticationToken greeting(@RequestParam(value = "login", defaultValue = Configuration.ANONYMOUS_LOGIN) String login,
@RequestParam(value = "password", required = false) String password) {
AuthenticationToken token = userService.login(login, password);
return token;
}
/**
* @return the userService
* @see #userService
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
* @see #userService
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
}
\ No newline at end of file
package lcsb.mapviewer.api.project;
import java.awt.geom.Point2D;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.primefaces.model.map.LatLng;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.map.layout.Layout;
import lcsb.mapviewer.model.map.model.Model;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.model.map.model.ModelSubmodelConnection;
import lcsb.mapviewer.services.utils.gmap.CoordinationConverter;
import lcsb.mapviewer.services.view.LayoutView;
import lcsb.mapviewer.services.view.LayoutViewFactory;
public class ModelMetaData implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Version of the model.
*/
private String version;
/**
* Name of the model.
*/
private String name;
/**
* Size in pixels of the single square tile (small image used for graphical
* representation).
*/
private Integer tileSize;
private Integer width;
private Integer height;
/**
* Minimum zoom level that should be allowed by the Google Maps API.
*/
private Integer minZoom;
/**
* Maximum zoom level that should be allowed by the Google Maps API.
*/
private Integer maxZoom;
private List<LayoutView> layouts = new ArrayList<>();
/**
* List of submodels in the model.
*/
private List<ModelMetaData> submodels = new ArrayList<>();
/**
* Where is the center of the map in latituted, longiude format.
*/
private LatLng centerLatLng;
/**
* Top-Left corner of the map (0,0) as a latLng coordinates.
*/
private LatLng topLeftLatLng;
/**
* Bottom-Right corner of the map (width,height) as a latLng coordinates.
*/
private LatLng bottomRightLatLng;
/**
* Default constructor.
*/
public ModelMetaData(Model model) {
this(model.getModelData());
}
public ModelMetaData(ModelData model) {
this.setName(model.getName());
this.setMinZoom(Configuration.MIN_ZOOM_LEVEL);
this.setMaxZoom(this.getMinZoom() + model.getZoomLevels());
this.setTileSize(model.getTileSize());
this.setWidth((int) (double) model.getWidth());
this.setHeight((int) (double) model.getHeight());
int size = Math.max(width, height);
CoordinationConverter cConverter = new CoordinationConverter(model);
this.setCenterLatLng(cConverter.toLatLng(new Point2D.Double(size / 2, size / 2)));
this.setBottomRightLatLng(cConverter.toLatLng(new Point2D.Double(model.getWidth(), model.getHeight())));
this.setTopLeftLatLng(cConverter.toLatLng(new Point2D.Double(0, 0)));
List<ModelMetaData> submodels = new ArrayList<>();
for (ModelSubmodelConnection connection : model.getSubmodels()) {
submodels.add(new ModelMetaData(connection.getSubmodel().getModel()));
}
LayoutViewFactory factory = new LayoutViewFactory();
for (Layout layout : model.getLayouts()) {
if (layout.isPublicLayout()) {
layouts.add(factory.create(layout));
}
}
this.setSubmodels(submodels);
}
protected ModelMetaData() {
}
/**
* @return the version
* @see #version
*/
public String getVersion() {
return version;
}
/**
* @param version
* the version to set
* @see #version
*/
public void setVersion(String version) {
this.version = version;
}
/**
* @return the tileSize
* @see #tileSize
*/
public Integer getTileSize() {
return tileSize;
}
/**
* @param tileSize
* the tileSize to set
* @see #tileSize
*/
public void setTileSize(Integer tileSize) {
this.tileSize = tileSize;
}
/**
* @return the minZoom
* @see #minZoom
*/
public Integer getMinZoom() {
return minZoom;
}
/**
* @param minZoom
* the minZoom to set
* @see #minZoom
*/
public void setMinZoom(Integer minZoom) {
this.minZoom = minZoom;
}
/**
* @return the maxZoom
* @see #maxZoom
*/
public Integer getMaxZoom() {
return maxZoom;
}
/**
* @param maxZoom
* the maxZoom to set
* @see #maxZoom
*/
public void setMaxZoom(Integer maxZoom) {
this.maxZoom = maxZoom;
}
/**
* @return the centerLatLng
* @see #centerLatLng
*/
public LatLng getCenterLatLng() {
return centerLatLng;
}
/**
* @param centerLatLng
* the centerLatLng to set
* @see #centerLatLng
*/
public void setCenterLatLng(LatLng centerLatLng) {
this.centerLatLng = centerLatLng;
}
/**
* @return the name
* @see #name
*/
public String getName() {
return name;
}
/**
* @param name
* the name to set
* @see #name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the topLeftLatLng
* @see #topLeftLatLng
*/
public LatLng getTopLeftLatLng() {
return topLeftLatLng;
}
/**
* @param topLeftLatLng
* the topLeftLatLng to set
* @see #topLeftLatLng
*/
public void setTopLeftLatLng(LatLng topLeftLatLng) {
this.topLeftLatLng = topLeftLatLng;
}
/**
* @return the bottomRightLatLng
* @see #bottomRightLatLng
*/
public LatLng getBottomRightLatLng() {
return bottomRightLatLng;
}
/**
* @param bottomRightLatLng
* the bottomRightLatLng to set
* @see #bottomRightLatLng
*/
public void setBottomRightLatLng(LatLng bottomRightLatLng) {
this.bottomRightLatLng = bottomRightLatLng;
}
/**
* @return the width
* @see #width
*/
public Integer getWidth() {
return width;
}
/**
* @param width
* the width to set
* @see #width
*/
public void setWidth(Integer width) {
this.width = width;
}
/**
* @return the height
* @see #height
*/
public Integer getHeight() {
return height;
}
/**
* @param height
* the height to set
* @see #height
*/
public void setHeight(Integer height) {
this.height = height;
}
/**
* @return the submodels
* @see #submodels
*/
public List<ModelMetaData> getSubmodels() {
return submodels;
}
/**
* @param submodels
* the submodels to set
* @see #submodels
*/
public void setSubmodels(List<ModelMetaData> submodels) {
this.submodels = submodels;
}
/**
* @return the layouts
* @see #layouts
*/
public List<LayoutView> getLayouts() {
return layouts;
}
/**
* @param layouts the layouts to set
* @see #layouts
*/
public void setLayouts(List<LayoutView> layouts) {
this.layouts = layouts;
}
}
package lcsb.mapviewer.api.project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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.RestController;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.services.SecurityException;
@RestController
@RequestMapping("/project")
public class ProjectController extends BaseController {
@Autowired
private ProjectRestImpl projectController;
@RequestMapping(value = "/getMetaData", method = { RequestMethod.GET, RequestMethod.POST }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ProjectMetaData getMetaData(@RequestParam(value = "projectId") String projectId, @RequestParam(value = "token") String token)
throws SecurityException {
return projectController.getMetaData(projectId, token);
}
}
\ No newline at end of file
package lcsb.mapviewer.api.project;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.log4j.Logger;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.OverviewImage;
import lcsb.mapviewer.model.map.OverviewImageLink;
import lcsb.mapviewer.model.map.OverviewLink;
import lcsb.mapviewer.model.map.model.ModelData;
import lcsb.mapviewer.services.view.OverviewImageView;
import lcsb.mapviewer.services.view.OverviewImageViewFactory;
public class ProjectMetaData implements Serializable {
private static Logger logger = Logger.getLogger(ProjectMetaData.class);
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Version of the project.
*/
private String version;
/**
* Name of the project.
*/
private String name;
/**
* {@link lcsb.mapviewer.model.Project#projectId Project identifier}.
*/
private String projectId;
/**
* Description of the model.
*/
private String description;
private ModelMetaData map;
/**
* List of overview images attached to this model.
*/
private List<OverviewImageView> overviewImageViews;
/**
* Top level overview image.
*/
private OverviewImageView topOverviewImage;
/**
* Default constructor. Should be used only for deserialization.
*/
protected ProjectMetaData() {
}