Skip to content

Commit

Permalink
Revert "Modularization: removing old src. See #5"
Browse files Browse the repository at this point in the history
This reverts commit 533435a.
  • Loading branch information
enridaga committed Apr 24, 2015
1 parent 533435a commit 62ec3d2
Show file tree
Hide file tree
Showing 59 changed files with 4,373 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/main/java/uk/ac/open/kmi/basil/AbstractResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package uk.ac.open.kmi.basil;

import java.net.HttpURLConnection;
import java.net.URI;

import javax.servlet.ServletContext;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriInfo;

import uk.ac.open.kmi.basil.store.Store;

public class AbstractResource {

@Context
protected HttpHeaders requestHeaders;

@Context
protected UriInfo requestUri;

@Context
protected ServletContext context;

protected final ResponseBuilder addHeaders(ResponseBuilder builder,
String id) {
URI api = requestUri.getBaseUriBuilder().path(id).path("api").build();
URI spec = requestUri.getBaseUriBuilder().path(id).path("spec").build();
// URI store = requestUri.getBaseUriBuilder().path(id).path("store")
// .build();
URI views = requestUri.getBaseUriBuilder().path(id).path("view")
.build();
URI swagger = requestUri.getBaseUriBuilder().path(id).path("api-docs")
.build();
URI docs = requestUri.getBaseUriBuilder().path(id).path("docs")
.build();
builder.header(Headers.Api, api);
builder.header(Headers.Spec, spec);
// builder.header(Headers.Store, store); XXX Not implemented Yet
builder.header(Headers.View, views);
builder.header(Headers.Docs, docs);
builder.header(Headers.Swagger, swagger);
return builder;
}

protected final String getParameterOrHeader(String parameter,
boolean mandatory) {
String value = requestUri.getQueryParameters().getFirst(parameter);
if (value == null) {
if (requestHeaders.getHeaderString(Headers.getHeader(parameter)) == null) {
throw new WebApplicationException(Response
.status(HttpURLConnection.HTTP_BAD_REQUEST)
.header(Headers.Error,
Headers.getHeader(parameter)
+ " (or query parameter '" + parameter
+ "') missing.").build());
} else {
value = requestHeaders.getHeaderString(Headers
.getHeader(parameter));
}
}
return value;
}

protected Store getDataStore() {
return (Store) context.getAttribute(BasilApplication.Registry.Store);
}
}
54 changes: 54 additions & 0 deletions src/main/java/uk/ac/open/kmi/basil/ApiDocsResource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package uk.ac.open.kmi.basil;

import java.io.IOException;

import javax.ws.rs.GET;
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.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import org.json.simple.JSONObject;

import uk.ac.open.kmi.basil.doc.Doc;
import uk.ac.open.kmi.basil.sparql.Specification;
import uk.ac.open.kmi.basil.store.Store;
import uk.ac.open.kmi.basil.swagger.SwaggerJsonBuilder;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiResponse;
import com.wordnik.swagger.annotations.ApiResponses;


@Path("{id:([^/]+)}/api-docs")
@Api(value = "/basil", description = "BASIL operations")
public class ApiDocsResource extends AbstractResource {

@GET
@Produces("application/json")
@ApiOperation(value = "Generates API Swagger Description")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 500, message = "Internal error")
})
public Response get(@PathParam("id") String id) {
try {
Store store = getDataStore();
if (!store.existsSpec(id)) {
return Response.status(404).build();
}

Specification specification = store.loadSpec(id);
Doc docs = store.loadDoc(id);
JSONObject o = SwaggerJsonBuilder.build(id, specification, docs, requestUri.getBaseUri().toString());
ResponseBuilder builder = Response.ok(o.toJSONString());
addHeaders(builder, id);
return builder.build();
} catch (IOException e) {
throw new WebApplicationException(e);
}
}
}
Loading

0 comments on commit 62ec3d2

Please sign in to comment.