-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
62 changed files
with
4,562 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
basil/src/main/java/uk/ac/open/kmi/basil/AbstractResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
basil/src/main/java/uk/ac/open/kmi/basil/ApiDocsResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.