Skip to content

Commit

Permalink
Modularization. See #5
Browse files Browse the repository at this point in the history
  • Loading branch information
enridaga committed Apr 24, 2015
1 parent 62ec3d2 commit a4d865f
Show file tree
Hide file tree
Showing 62 changed files with 4,562 additions and 28 deletions.
34 changes: 6 additions & 28 deletions basil/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>basil</groupId>
<artifactId>parent</artifactId>
<version>0.2.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<groupId>basil</groupId>
<artifactId>basil</artifactId>
<version>0.2.0-SNAPSHOT</version>
Expand All @@ -12,39 +18,25 @@

<description>Tool for Building web Apis SImpLy on top of sparql endpoints</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- Versions -->
<slf4j.version>1.7.7</slf4j.version>
<!-- current log4j 2 release -->
<log4j.version>2.1</log4j.version>
<!-- jetty -->
<org.eclipse.jetty.version>9.2.3.v20140905</org.eclipse.jetty.version>
</properties>

<dependencies>
<!-- Logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Binding for Log4J -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- Log4j API and Core implementation required for binding -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>

<!-- Test -->
Expand All @@ -59,22 +51,18 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>com.github.spullara.mustache.java</groupId>
<artifactId>compiler</artifactId>
<version>0.9.0</version>
</dependency>
<dependency>
<groupId>org.mozilla</groupId>
<artifactId>rhino</artifactId>
<version>1.7R5</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
Expand All @@ -89,17 +77,14 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.12.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
Expand All @@ -114,7 +99,6 @@
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.12.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
Expand All @@ -131,32 +115,27 @@
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.14</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${org.eclipse.jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${org.eclipse.jetty.version}</version>
</dependency>

<!-- Standalone -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>

<!-- Swagger -->
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
<scope>compile</scope>
<version>1.3.12</version>
<exclusions>
<exclusion>
<groupId>javax.ws.rs</groupId>
Expand All @@ -167,7 +146,6 @@
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>1.3.2</version>
<exclusions>
<exclusion>
<artifactId>servlet-api</artifactId>
Expand Down
70 changes: 70 additions & 0 deletions basil/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 basil/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 a4d865f

Please sign in to comment.