Skip to content

Commit

Permalink
Documents for algorithmic resources has been updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
keiono committed Aug 14, 2014
1 parent 1fb7600 commit 414ffac
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 72 deletions.
41 changes: 36 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
<name>Cytoscape Releases</name>
<url>http://code.cytoscape.org/nexus/content/repositories/releases/</url>
</repository>


<!-- For API document generation -->
<repository>
<id>miredot</id>
<name>MireDot Releases</name>
Expand Down Expand Up @@ -111,9 +112,6 @@
<!-- insert other configuration here (optional) -->
<output>
<title>RESTful API for Cytoscape ${rest.api.version}</title>
<html>
<jsonDoc>Disabled</jsonDoc>
</html>
</output>


Expand All @@ -127,14 +125,47 @@
<httpStatusCode>
<httpCode>412</httpCode>
<document>put,post</document>
<defaultMessage>Invalid JSON/XML input.</defaultMessage>
<defaultMessage>Invalid JSON input.</defaultMessage>
</httpStatusCode>
</httpStatusCodes>
</restModel>

</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>com.qmino</groupId>
<artifactId>
miredot-maven-plugin
</artifactId>
<versionRange>
[1.4,)
</versionRange>
<goals>
<goal>restdoc</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>


Expand Down
15 changes: 15 additions & 0 deletions src/main/java/org/cytoscape/rest/internal/model/TableCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cytoscape.rest.internal.model;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;

/**
* Wrapper class for a cell in CyTable
*
* @author kono
*
*/
//@JsonSerialize(using=)
public class TableCell {


}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private final void serializeRow(JsonGenerator generator, final CyIdentifiable ob
final Long targetId = ((CyEdge) obj).getSource().getSUID();
generator.writeNumberField("source", sourceId);
generator.writeNumberField("target", targetId);
} else {
generator.writeNumberField("id", obj.getSUID());
}
for (final CyColumn col : columns) {
final Object value = values.get(col.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.Properties;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;

import org.cytoscape.application.CyApplicationManager;
import org.cytoscape.io.read.InputStreamTaskFactory;
Expand Down Expand Up @@ -41,7 +43,21 @@
public abstract class AbstractDataService {

// TODO: do we need this level of version granularity?
protected static final String API_VERSION = "1.0.0";
protected static final String API_VERSION = "v1";

protected static final String ERROR_TAG = "\"error\":";


// Utilities to build error messages.

protected final WebApplicationException getError(final String errorMessage, final Status status) {
return new NotFoundException(Response.status(status)
.entity("{" + ERROR_TAG + "\"" + errorMessage + "\"}").build());
}

protected final WebApplicationException getError(final Exception ex, final Status status) {
return new NotFoundException(Response.status(status).entity(ex).build());
}


@Context
Expand Down Expand Up @@ -100,12 +116,12 @@ public AbstractDataService() {

protected final CyNetwork getCyNetwork(final Long id) {
if(id == null) {
throw new WebApplicationException("Network SUID is null.", 500);
throw getError("SUID is null.", Response.Status.NOT_FOUND);
}

final CyNetwork network = networkManager.getNetwork(id);
if (network == null) {
throw new WebApplicationException("Could not find network with SUID: " + id, 404);
throw getError("Could not find network with SUID: " + id, Response.Status.NOT_FOUND);
}
return network;
}
Expand All @@ -123,7 +139,7 @@ protected final Collection<CyNetworkView> getCyNetworkViews(final Long id) {
protected final CyNode getNode(final CyNetwork network, final Long nodeId) {
final CyNode node = network.getNode(nodeId);
if (node == null) {
throw new WebApplicationException("Could not find object: " + nodeId, 404);
throw getError("Could not find node with SUID: " + nodeId, Response.Status.NOT_FOUND);
}
return node;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package org.cytoscape.rest.internal.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import javax.activation.MimetypesFileTypeMap;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
Expand All @@ -27,105 +22,137 @@
import org.cytoscape.work.TaskIterator;
import org.cytoscape.work.TaskMonitor;



/**
* Algorithmic resources. Essentially, this is a high-level task executor.
*
* @author kono
*
*/
@Singleton
@Path("/v1/apply")
public class AlgorithmicService extends AbstractDataService {

@Context
private TaskMonitor headlessTaskMonitor;

@Context
private CyLayoutAlgorithmManager layoutManager;


/**
*
* @summary Apply layout to a network
*
* @param algorithmName Name of layout algorithm ("circular", "force-directed", etc.)
* @param networkId Target network SUID
*
* @return Success message
*/
@GET
@Path("/layouts/{algorithmName}/{targetId}")
@Path("/layouts/{algorithmName}/{networkId}")
@Produces(MediaType.APPLICATION_JSON)
public String applyLayout(@PathParam("algorithmName") String algorithmName, @PathParam("targetId") Long targetId) {

final CyNetwork network = getCyNetwork(targetId);
Collection<CyNetworkView> views = this.networkViewManager.getNetworkViews(network);
public Response applyLayout(@PathParam("algorithmName") String algorithmName, @PathParam("networkId") Long networkId) {
final CyNetwork network = getCyNetwork(networkId);
final Collection<CyNetworkView> views = this.networkViewManager.getNetworkViews(network);
if (views.isEmpty()) {
throw new NotFoundException("View is not available for the network " + targetId);
throw getError("Could not find view for the network with SUID: " + networkId, Response.Status.NOT_FOUND);
}

final CyNetworkView view = views.iterator().next();
final CyLayoutAlgorithm layout = this.layoutManager.getLayout(algorithmName);

if(layout == null) {
throw getError("No such layout algorithm: " + algorithmName, Response.Status.NOT_FOUND);
}

final TaskIterator itr = layout.createTaskIterator(view, layout.getDefaultLayoutContext(),
CyLayoutAlgorithm.ALL_NODE_VIEWS, "");
try {
itr.next().run(headlessTaskMonitor);
} catch (Exception e) {
e.printStackTrace();
throw getError(e, Response.Status.INTERNAL_SERVER_ERROR);
}
return "{\"status\":\"OK\"}";
return Response.status(Response.Status.OK).entity("{\"message\":\"Layout finished.\"}").build();
}




/**
* Apply Visual Style to a network.
*
* @param styleName Visual Style name (title)
* @param networkId Target network SUID
*
* @return Success message.
*/
@GET
@Path("/styles/{styleName}/{targetId}")
@Path("/styles/{styleName}/{networkId}")
@Produces(MediaType.APPLICATION_JSON)
public String applyStyle(@PathParam("styleName") String styleName, @PathParam("targetId") Long targetId) {
public Response applyStyle(@PathParam("styleName") String styleName, @PathParam("networkId") Long networkId) {

final CyNetwork network = getCyNetwork(targetId);
final CyNetwork network = getCyNetwork(networkId);
final Set<VisualStyle> styles = vmm.getAllVisualStyles();
VisualStyle targetStyle = null;
for(final VisualStyle style:styles) {
for (final VisualStyle style : styles) {
final String name = style.getTitle();
if(name.equals(styleName)) {
if (name.equals(styleName)) {
targetStyle = style;
break;
}
}
if(targetStyle == null) {
throw new NotFoundException("Could not find Visual Style: " + styleName);

if (targetStyle == null) {
throw getError("Visual Style does not exist: " + styleName, Response.Status.NOT_FOUND);
}

Collection<CyNetworkView> views = this.networkViewManager.getNetworkViews(network);
if (views.isEmpty()) {
throw new NotFoundException("View is not available for the network " + targetId);
throw getError("Network view does not exist for the network with SUID: " + networkId, Response.Status.NOT_FOUND);
}


final CyNetworkView view = views.iterator().next();
vmm.setVisualStyle(targetStyle, view);
vmm.setCurrentVisualStyle(targetStyle);
targetStyle.apply(view);
return "{\"status\":\"OK\"}";

return Response.status(Response.Status.OK).entity("{\"message\":\"Visual Style applied.\"}").build();
}

/**
* @summary Get list of available layout algorithm names
*
* @return List of layout algorithm names.
*/
@GET
@Path("/layouts")
@Produces(MediaType.APPLICATION_JSON)
public String getLayouts() {
public Collection<String> getLayoutNames() {
final Collection<CyLayoutAlgorithm> layouts = layoutManager.getAllLayouts();
final List<String> layoutNames= new ArrayList<String>();
for(final CyLayoutAlgorithm layout: layouts) {
final List<String> layoutNames = new ArrayList<String>();
for (final CyLayoutAlgorithm layout : layouts) {
layoutNames.add(layout.getName());
}
try {
return getNames(layoutNames);
} catch (IOException e) {
throw new WebApplicationException(e, 500);
}
return layoutNames;
}




/**
* @summary Get list of all Visual Style names.
*
* @return List of Style names.
*
*/
@GET
@Path("/images/{image}")
@Produces("image/*")
public Response getImage(@PathParam("image") String image) {
// TODO implement this
File f = new File(image);


if (!f.exists()) {
throw new WebApplicationException(404);
@Path("/styles")
@Produces(MediaType.APPLICATION_JSON)
public Collection<String> getStyleNames() {
final Set<VisualStyle> styles = vmm.getAllVisualStyles();
final List<String> styleNames = new ArrayList<String>();
for (final VisualStyle style : styles) {
styleNames.add(style.getTitle());
}

final String mt = new MimetypesFileTypeMap().getContentType(f);
return Response.ok(f, mt).build();
return styleNames;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.cytoscape.io.read.CyNetworkReader;
import org.cytoscape.io.write.CyWriter;
Expand Down Expand Up @@ -302,7 +303,7 @@ public String getEdge(@PathParam("networkId") Long networkId, @PathParam("edgeId
final CyNetwork network = getCyNetwork(networkId);
final CyEdge edge = network.getEdge(edgeId);
if (edge == null) {
throw new NotFoundException("Could not find edge with SUID: " + edgeId);
throw getError("Could not find edge with SUID: " + edgeId, Response.Status.NOT_FOUND);
}
return getGraphObject(network, edge);
}
Expand Down Expand Up @@ -854,11 +855,11 @@ private final String getNetworkString(final CyNetwork network) {
CyWriter writer = cytoscapeJsWriterFactory.createWriter(stream, network);
String jsonString = null;
try {
writer.run(null);
writer.run(new HeadlessTaskMonitor());
jsonString = stream.toString();
stream.close();
} catch (Exception e) {
e.printStackTrace();
throw getError(e, Response.Status.PRECONDITION_FAILED);
}
return jsonString;
}
Expand Down
Loading

0 comments on commit 414ffac

Please sign in to comment.