Skip to content

Commit a4d865f

Browse files
committed
Modularization. See #5
1 parent 62ec3d2 commit a4d865f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4562
-28
lines changed

basil/pom.xml

+6-28
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6+
<parent>
7+
<groupId>basil</groupId>
8+
<artifactId>parent</artifactId>
9+
<version>0.2.0-SNAPSHOT</version>
10+
<relativePath>../parent</relativePath>
11+
</parent>
612
<groupId>basil</groupId>
713
<artifactId>basil</artifactId>
814
<version>0.2.0-SNAPSHOT</version>
@@ -12,39 +18,25 @@
1218

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

15-
<properties>
16-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17-
<!-- Versions -->
18-
<slf4j.version>1.7.7</slf4j.version>
19-
<!-- current log4j 2 release -->
20-
<log4j.version>2.1</log4j.version>
21-
<!-- jetty -->
22-
<org.eclipse.jetty.version>9.2.3.v20140905</org.eclipse.jetty.version>
23-
</properties>
24-
2521
<dependencies>
2622
<!-- Logging -->
2723
<dependency>
2824
<groupId>org.slf4j</groupId>
2925
<artifactId>slf4j-api</artifactId>
30-
<version>${slf4j.version}</version>
3126
</dependency>
3227
<!-- Binding for Log4J -->
3328
<dependency>
3429
<groupId>org.apache.logging.log4j</groupId>
3530
<artifactId>log4j-slf4j-impl</artifactId>
36-
<version>${log4j.version}</version>
3731
</dependency>
3832
<!-- Log4j API and Core implementation required for binding -->
3933
<dependency>
4034
<groupId>org.apache.logging.log4j</groupId>
4135
<artifactId>log4j-api</artifactId>
42-
<version>${log4j.version}</version>
4336
</dependency>
4437
<dependency>
4538
<groupId>org.apache.logging.log4j</groupId>
4639
<artifactId>log4j-core</artifactId>
47-
<version>${log4j.version}</version>
4840
</dependency>
4941

5042
<!-- Test -->
@@ -59,22 +51,18 @@
5951
<dependency>
6052
<groupId>javax.servlet</groupId>
6153
<artifactId>javax.servlet-api</artifactId>
62-
<version>3.1.0</version>
6354
</dependency>
6455
<dependency>
6556
<groupId>com.github.spullara.mustache.java</groupId>
6657
<artifactId>compiler</artifactId>
67-
<version>0.9.0</version>
6858
</dependency>
6959
<dependency>
7060
<groupId>org.mozilla</groupId>
7161
<artifactId>rhino</artifactId>
72-
<version>1.7R5</version>
7362
</dependency>
7463
<dependency>
7564
<groupId>commons-io</groupId>
7665
<artifactId>commons-io</artifactId>
77-
<version>2.4</version>
7866
<exclusions>
7967
<exclusion>
8068
<groupId>log4j</groupId>
@@ -89,17 +77,14 @@
8977
<dependency>
9078
<groupId>org.apache.commons</groupId>
9179
<artifactId>commons-collections4</artifactId>
92-
<version>4.0</version>
9380
</dependency>
9481
<dependency>
9582
<groupId>com.googlecode.json-simple</groupId>
9683
<artifactId>json-simple</artifactId>
97-
<version>1.1.1</version>
9884
</dependency>
9985
<dependency>
10086
<groupId>org.apache.jena</groupId>
10187
<artifactId>jena-core</artifactId>
102-
<version>2.12.1</version>
10388
<exclusions>
10489
<exclusion>
10590
<groupId>log4j</groupId>
@@ -114,7 +99,6 @@
11499
<dependency>
115100
<groupId>org.apache.jena</groupId>
116101
<artifactId>jena-arq</artifactId>
117-
<version>2.12.1</version>
118102
<exclusions>
119103
<exclusion>
120104
<groupId>log4j</groupId>
@@ -131,32 +115,27 @@
131115
<dependency>
132116
<groupId>org.glassfish.jersey.containers</groupId>
133117
<artifactId>jersey-container-servlet</artifactId>
134-
<version>2.14</version>
135118
</dependency>
136119
<dependency>
137120
<groupId>org.eclipse.jetty</groupId>
138121
<artifactId>jetty-server</artifactId>
139-
<version>${org.eclipse.jetty.version}</version>
140122
</dependency>
141123
<dependency>
142124
<groupId>org.eclipse.jetty</groupId>
143125
<artifactId>jetty-webapp</artifactId>
144-
<version>${org.eclipse.jetty.version}</version>
145126
</dependency>
146127

147128
<!-- Standalone -->
148129
<dependency>
149130
<groupId>commons-cli</groupId>
150131
<artifactId>commons-cli</artifactId>
151-
<version>1.2</version>
152132
</dependency>
153133

154134
<!-- Swagger -->
155135
<dependency>
156136
<groupId>com.wordnik</groupId>
157137
<artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
158138
<scope>compile</scope>
159-
<version>1.3.12</version>
160139
<exclusions>
161140
<exclusion>
162141
<groupId>javax.ws.rs</groupId>
@@ -167,7 +146,6 @@
167146
<dependency>
168147
<groupId>com.thetransactioncompany</groupId>
169148
<artifactId>cors-filter</artifactId>
170-
<version>1.3.2</version>
171149
<exclusions>
172150
<exclusion>
173151
<artifactId>servlet-api</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package uk.ac.open.kmi.basil;
2+
3+
import java.net.HttpURLConnection;
4+
import java.net.URI;
5+
6+
import javax.servlet.ServletContext;
7+
import javax.ws.rs.WebApplicationException;
8+
import javax.ws.rs.core.Context;
9+
import javax.ws.rs.core.HttpHeaders;
10+
import javax.ws.rs.core.Response;
11+
import javax.ws.rs.core.Response.ResponseBuilder;
12+
import javax.ws.rs.core.UriInfo;
13+
14+
import uk.ac.open.kmi.basil.store.Store;
15+
16+
public class AbstractResource {
17+
18+
@Context
19+
protected HttpHeaders requestHeaders;
20+
21+
@Context
22+
protected UriInfo requestUri;
23+
24+
@Context
25+
protected ServletContext context;
26+
27+
protected final ResponseBuilder addHeaders(ResponseBuilder builder,
28+
String id) {
29+
URI api = requestUri.getBaseUriBuilder().path(id).path("api").build();
30+
URI spec = requestUri.getBaseUriBuilder().path(id).path("spec").build();
31+
// URI store = requestUri.getBaseUriBuilder().path(id).path("store")
32+
// .build();
33+
URI views = requestUri.getBaseUriBuilder().path(id).path("view")
34+
.build();
35+
URI swagger = requestUri.getBaseUriBuilder().path(id).path("api-docs")
36+
.build();
37+
URI docs = requestUri.getBaseUriBuilder().path(id).path("docs")
38+
.build();
39+
builder.header(Headers.Api, api);
40+
builder.header(Headers.Spec, spec);
41+
// builder.header(Headers.Store, store); XXX Not implemented Yet
42+
builder.header(Headers.View, views);
43+
builder.header(Headers.Docs, docs);
44+
builder.header(Headers.Swagger, swagger);
45+
return builder;
46+
}
47+
48+
protected final String getParameterOrHeader(String parameter,
49+
boolean mandatory) {
50+
String value = requestUri.getQueryParameters().getFirst(parameter);
51+
if (value == null) {
52+
if (requestHeaders.getHeaderString(Headers.getHeader(parameter)) == null) {
53+
throw new WebApplicationException(Response
54+
.status(HttpURLConnection.HTTP_BAD_REQUEST)
55+
.header(Headers.Error,
56+
Headers.getHeader(parameter)
57+
+ " (or query parameter '" + parameter
58+
+ "') missing.").build());
59+
} else {
60+
value = requestHeaders.getHeaderString(Headers
61+
.getHeader(parameter));
62+
}
63+
}
64+
return value;
65+
}
66+
67+
protected Store getDataStore() {
68+
return (Store) context.getAttribute(BasilApplication.Registry.Store);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package uk.ac.open.kmi.basil;
2+
3+
import java.io.IOException;
4+
5+
import javax.ws.rs.GET;
6+
import javax.ws.rs.Path;
7+
import javax.ws.rs.PathParam;
8+
import javax.ws.rs.Produces;
9+
import javax.ws.rs.WebApplicationException;
10+
import javax.ws.rs.core.Response;
11+
import javax.ws.rs.core.Response.ResponseBuilder;
12+
13+
import org.json.simple.JSONObject;
14+
15+
import uk.ac.open.kmi.basil.doc.Doc;
16+
import uk.ac.open.kmi.basil.sparql.Specification;
17+
import uk.ac.open.kmi.basil.store.Store;
18+
import uk.ac.open.kmi.basil.swagger.SwaggerJsonBuilder;
19+
20+
import com.wordnik.swagger.annotations.Api;
21+
import com.wordnik.swagger.annotations.ApiOperation;
22+
import com.wordnik.swagger.annotations.ApiResponse;
23+
import com.wordnik.swagger.annotations.ApiResponses;
24+
25+
26+
@Path("{id:([^/]+)}/api-docs")
27+
@Api(value = "/basil", description = "BASIL operations")
28+
public class ApiDocsResource extends AbstractResource {
29+
30+
@GET
31+
@Produces("application/json")
32+
@ApiOperation(value = "Generates API Swagger Description")
33+
@ApiResponses(value = {
34+
@ApiResponse(code = 200, message = "OK"),
35+
@ApiResponse(code = 500, message = "Internal error")
36+
})
37+
public Response get(@PathParam("id") String id) {
38+
try {
39+
Store store = getDataStore();
40+
if (!store.existsSpec(id)) {
41+
return Response.status(404).build();
42+
}
43+
44+
Specification specification = store.loadSpec(id);
45+
Doc docs = store.loadDoc(id);
46+
JSONObject o = SwaggerJsonBuilder.build(id, specification, docs, requestUri.getBaseUri().toString());
47+
ResponseBuilder builder = Response.ok(o.toJSONString());
48+
addHeaders(builder, id);
49+
return builder.build();
50+
} catch (IOException e) {
51+
throw new WebApplicationException(e);
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)