Skip to content
Snippets Groups Projects
Commit 9a5d7e7b authored by Piotr Gawron's avatar Piotr Gawron
Browse files

new api call format for removing overlay

parent d3e49019
No related branches found
No related tags found
1 merge request!11Resolve "Rest API should follow google guidlines"
Showing
with 168 additions and 62 deletions
......@@ -35,6 +35,9 @@
<jersey.version>1.18.1</jersey.version>
<rs-jax.version>1.1.1</rs-jax.version>
<jackson.version>2.8.8</jackson.version>
<log4j.version>1.2.17</log4j.version>
<apache.commons-lang3.version>3.1</apache.commons-lang3.version>
......
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="org.eclipse.jst.component.nondependency" value=""/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
......@@ -2,6 +2,5 @@
<wb-module deploy-name="MapViewer-rest-api">
<wb-resource deploy-path="/" source-path="/src/main/java"/>
<wb-resource deploy-path="/" source-path="/src/main/resources"/>
<wb-resource deploy-path="/" source-path="/src/test/resources"/>
</wb-module>
</project-modules>
......@@ -48,6 +48,18 @@
<artifactId>spring-faces</artifactId>
<version>${springframework.webflow.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
......
......@@ -18,6 +18,9 @@ public abstract class BaseController {
logger.error(e, e);
if (e instanceof SecurityException) {
return new ResponseEntity<Object>("{\"error\" : \"Access denied.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(), HttpStatus.FORBIDDEN);
} else if (e instanceof ObjectNotFoundException) {
return new ResponseEntity<Object>(
"{\"error\" : \"Object not found.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(), HttpStatus.NOT_FOUND);
} else if (e instanceof QueryException) {
return new ResponseEntity<Object>(
"{\"error\" : \"Query server error.\",\"reason\":\"" + e.getMessage() + "\"}", new HttpHeaders(), HttpStatus.BAD_REQUEST);
......
......@@ -26,7 +26,6 @@ public abstract class BaseRestImpl {
protected Map<String, Object> okStatus() {
Map<String, Object> result = new HashMap<>();
result.put("status", "OK");
return result;
}
......
package lcsb.mapviewer.api;
public class ObjectNotFoundException extends QueryException {
public ObjectNotFoundException(String message) {
super(message);
}
public ObjectNotFoundException(String message, Exception reason) {
super(message, reason);
}
}
......@@ -4,17 +4,20 @@ import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.ws.rs.PathParam;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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 com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.model.cache.FileEntry;
......@@ -24,15 +27,19 @@ import lcsb.mapviewer.services.view.LayoutView;
@RestController
public class OverlayController extends BaseController {
private Logger logger = Logger.getLogger(OverlayController.class);
@Autowired
private OverlayRestImpl overlayController;
private OverlayRestImpl overlayRestImp;
private ObjectMapper mapper = new ObjectMapper();
@RequestMapping(value = "/projects/{projectId}/overlays/", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<LayoutView> getOverlayList(//
@RequestParam(value = "token") String token, //
@PathVariable(value = "projectId") String projectId //
) throws SecurityException, QueryException {
return overlayController.getOverlayList(token, projectId);
return overlayRestImp.getOverlayList(token, projectId);
}
@RequestMapping(value = "/projects/{projectId}/overlays/{overlayId}/", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
......@@ -41,7 +48,7 @@ public class OverlayController extends BaseController {
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "overlayId") String overlayId//
) throws SecurityException, QueryException {
return overlayController.getOverlayById(token, projectId, overlayId);
return overlayRestImp.getOverlayById(token, projectId, overlayId);
}
@RequestMapping(value = "/projects/{projectId}/overlays/{overlayId}/models/{modelId}/bioEntities/", method = { RequestMethod.GET },
......@@ -51,7 +58,7 @@ public class OverlayController extends BaseController {
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "overlayId") String overlayId, @RequestParam(value = "columns", defaultValue = "") String columns)
throws SecurityException, QueryException {
return overlayController.getOverlayElements(token, projectId, Integer.valueOf(overlayId), columns);
return overlayRestImp.getOverlayElements(token, projectId, Integer.valueOf(overlayId), columns);
}
@RequestMapping(value = "/projects/{projectId}/overlays/{overlayId}/models/{modelId}/bioEntities/reactions/{reactionId}/", method = { RequestMethod.GET },
......@@ -64,7 +71,7 @@ public class OverlayController extends BaseController {
@PathVariable(value = "reactionId") String reactionId, //
@RequestParam(value = "columns", defaultValue = "") String columns //
) throws SecurityException, QueryException {
return overlayController
return overlayRestImp
.getOverlayElement(token, projectId, Integer.valueOf(modelId), Integer.valueOf(overlayId), Integer.valueOf(reactionId), "REACTION", columns);
}
......@@ -78,7 +85,7 @@ public class OverlayController extends BaseController {
@PathVariable(value = "elementId") String reactionId, //
@RequestParam(value = "columns", defaultValue = "") String columns //
) throws SecurityException, QueryException {
return overlayController
return overlayRestImp
.getOverlayElement(token, projectId, Integer.valueOf(modelId), Integer.valueOf(overlayId), Integer.valueOf(reactionId), "ALIAS", columns);
}
......@@ -92,12 +99,22 @@ public class OverlayController extends BaseController {
@RequestParam(value = "filename") String filename, //
@RequestParam(value = "type", defaultValue = "") String type //
) throws SecurityException, QueryException, IOException {
return overlayController.addOverlay(token, projectId, name, description, content, filename, type);
return overlayRestImp.addOverlay(token, projectId, name, description, content, filename, type);
}
@RequestMapping(value = "/projects/{projectId}/overlays/{overlayId}", method = { RequestMethod.DELETE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> removeOverlay( //
@RequestBody String body, //
@PathVariable(value = "projectId") String projectId, //
@PathVariable(value = "overlayId") String overlayId //
) throws SecurityException, QueryException, IOException {
ObjectNode node = mapper.readValue(body, ObjectNode.class);
return overlayRestImp.removeOverlay(node.get("token").asText(), projectId, overlayId);
}
@RequestMapping(value = "/overlay/getOverlayTypes", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public List<Map<String, Object>> getOverlayTypes(@RequestParam(value = "token") String token) throws SecurityException, QueryException {
return overlayController.getOverlayTypes(token);
return overlayRestImp.getOverlayTypes(token);
}
@RequestMapping(value = "/overlay/updateOverlay", method = { RequestMethod.GET, RequestMethod.PUT, RequestMethod.POST },
......@@ -105,21 +122,14 @@ public class OverlayController extends BaseController {
public Map<String, Object> updateOverlay(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "overlayId") String overlayId, @RequestParam(value = "name") String name, @RequestParam(value = "description") String description)
throws SecurityException, QueryException {
return overlayController.updateOverlay(token, projectId, overlayId, name, description);
}
@RequestMapping(value = "/overlay/removeOverlay", method = { RequestMethod.GET, RequestMethod.DELETE, RequestMethod.POST },
produces = { MediaType.APPLICATION_JSON_VALUE })
public Map<String, Object> removeOverlay(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "overlayId") String overlayId) throws SecurityException, QueryException, IOException {
return overlayController.removeOverlay(token, projectId, overlayId);
return overlayRestImp.updateOverlay(token, projectId, overlayId, name, description);
}
@RequestMapping(value = "/overlay/getOverlaySource", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<byte[]> getOverlaySource(@RequestParam(value = "token") String token, @RequestParam(value = "projectId") String projectId,
@RequestParam(value = "overlayId") String overlayId) throws SecurityException, QueryException {
FileEntry file = overlayController.getOverlaySource(token, projectId, overlayId);
FileEntry file = overlayRestImp.getOverlaySource(token, projectId, overlayId);
MediaType type = MediaType.TEXT_PLAIN;
if (file.getOriginalFileName().endsWith("xml")) {
type = MediaType.APPLICATION_XML;
......@@ -130,20 +140,20 @@ public class OverlayController extends BaseController {
}
/**
* @return the overlayController
* @see #overlayController
* @return the overlayRestImp
* @see #overlayRestImp
*/
public OverlayRestImpl getOverlayController() {
return overlayController;
public OverlayRestImpl getOverlayRestImp() {
return overlayRestImp;
}
/**
* @param overlayController
* the overlayController to set
* @see #overlayController
* @param overlayRestImp
* the overlayRestImp to set
* @see #overlayRestImp
*/
public void setOverlayController(OverlayRestImpl overlayController) {
this.overlayController = overlayController;
public void setOverlayRestImp(OverlayRestImpl overlayRestImp) {
this.overlayRestImp = overlayRestImp;
}
}
\ No newline at end of file
......@@ -14,6 +14,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.ObjectNotFoundException;
import lcsb.mapviewer.api.QueryException;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.cache.FileEntry;
......@@ -194,17 +195,17 @@ public class OverlayRestImpl extends BaseRestImpl {
AuthenticationToken authenticationToken = userService.getToken(token);
Model model = modelService.getLastModelByProjectId(projectId, authenticationToken);
if (model == null) {
throw new QueryException("Project with given id doesn't exist");
throw new ObjectNotFoundException("Project with given id doesn't exist");
}
try {
Integer id = Integer.valueOf(overlayId);
LayoutView layout = layoutService.getLayoutById(model, id, authenticationToken);
if (layout == null) {
throw new QueryException("Invalid overlay id");
throw new ObjectNotFoundException("Overlay doesn't exist");
}
layoutService.removeLayout(layout, null);
} catch (NumberFormatException e) {
throw new QueryException("Invalid overlay id");
throw new QueryException("Invalid overlay id: " + overlayId);
}
return okStatus();
}
......
......@@ -5,9 +5,11 @@ import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.api.projects.models.AllModelsTests;
import lcsb.mapviewer.api.projects.overlays.AllOverlaysTests;
@RunWith(Suite.class)
@SuiteClasses({ AllModelsTests.class, //
AllOverlaysTests.class, //
ModelMetaDataTest.class, //
ProjectRestImplTest.class })
public class AllProjectTests {
......
package lcsb.mapviewer.api.projects.overlays;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ OverlayControllerTest.class })
public class AllOverlaysTests {
}
package lcsb.mapviewer.api.projects.overlays;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.api.RestTestFunctions;
import lcsb.mapviewer.services.InvalidTokenException;
public class OverlayControllerTest extends RestTestFunctions {
OverlayController controller = new OverlayController();
@Autowired
OverlayRestImpl overlayRestImp;
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
controller.setOverlayRestImp(overlayRestImp);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testTokenParsing() throws Exception {
String body = "{\"token\":\"" + token + "\"}";
try {
controller.removeOverlay(body, "p_id", "-1");
fail("Exception expected");
} catch (InvalidTokenException e) {
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment