use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.
the class ReferencedSiteRootResource method handleCorsPreflightEntity.
@OPTIONS
@Path("/entity")
public Response handleCorsPreflightEntity(@PathParam(value = "site") String siteId, @Context HttpHeaders headers) {
Site site = getSite(siteId);
ResponseBuilder res = Response.ok();
if (site instanceof ManagedSite) {
//enableCORS(servletContext, res, headers, OPTIONS,GET,POST,PUT,DELETE);
} else {
//enableCORS(servletContext, res, headers,OPTIONS,GET);
}
return res.build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.
the class SiteManagerRootResource method executeLDPathQuery.
/**
* Execute a Query that uses LDPath to process results.
* @param query the query
* @param mediaType the mediaType for the response
* @param headers the http headers of the request
* @return the response
*/
private Response executeLDPathQuery(SiteManager manager, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
QueryResultList<Representation> result;
ValueFactory vf = new RdfValueFactory(new IndexedGraph());
SiteManagerBackend backend = new SiteManagerBackend(manager);
EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
//copy the selected fields, because we might need to delete some during
//the preparation phase
Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
//first prepare (only execute the query if the parameters are valid)
Program<Object> program;
try {
program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
} catch (LDPathParseException e) {
log.warn("Unable to parse LDPath program used as select for a Query to the '/sites' endpoint:");
log.warn("FieldQuery: \n {}", query);
log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
log.warn("Exception:", e);
return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
} catch (IllegalStateException e) {
log.warn("parsed LDPath program is not compatible with the Query " + "parsed to the '/sites' endpoint!", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
}
//2. execute the query
// we need to adapt from Entity to Representation
//TODO: should we add the metadata to the result?
Iterator<Representation> resultIt = new AdaptingIterator<Entity, Representation>(manager.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {
@Override
public Representation adapt(Entity value, Class<Representation> type) {
return value.getRepresentation();
}
}, Representation.class);
//process the results
Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
ResponseBuilder rb = Response.ok(result);
rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.
the class SiteManagerRootResource method getEntityById.
/**
* Cool URI handler for Signs.
*
* @param id
* The id of the entity (required)
* @param headers
* the request headers used to get the requested {@link MediaType}
* @return a redirection to either a browser view, the RDF meta data or the raw binary content
*/
@GET
@Path("/entity")
public Response getEntityById(@QueryParam(value = "id") String id, @Context HttpHeaders headers) {
log.debug("getSignById() request\n\t> id : {}\n\t> accept : {}\n\t> mediaType: {}", new Object[] { id, headers.getAcceptableMediaTypes(), headers.getMediaType() });
Collection<String> supported = new HashSet<String>(JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES);
supported.add(TEXT_HTML);
final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
if (id == null || id.isEmpty()) {
if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
ResponseBuilder rb = Response.ok(new Viewable("entity", this));
rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
} else {
return Response.status(Status.BAD_REQUEST).entity("No or empty ID was parsed. Missing parameter id.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
Entity sign = referencedSiteManager.getEntity(id);
if (sign != null) {
ResponseBuilder rb = Response.ok(sign);
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
} else {
// TODO: How to parse an ErrorMessage?
// create an Response with the the Error?
log.info("getSignById() entity {} not found on any referenced site");
return Response.status(Status.NOT_FOUND).entity("Entity with ID '" + id + "' not found an any referenced site\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.
the class ReferencedSiteRootResource method getLicenseInfo.
@GET
@Path(value = ReferencedSiteRootResource.LICENSE_PATH + "/{name}")
public Response getLicenseInfo(@PathParam(value = "site") String siteId, @Context HttpHeaders headers, @Context UriInfo uriInfo, @PathParam(value = "name") String name) {
Site site = getSite(siteId);
MediaType acceptedMediaType = getAcceptableMediaType(headers, MediaType.APPLICATION_JSON_TYPE);
if (name == null || name.isEmpty()) {
//return all
} else if (name.startsWith(LICENSE_NAME)) {
try {
String numberString = name.substring(LICENSE_NAME.length());
if (numberString.isEmpty()) {
numberString = "0";
}
//license0 is the first one
int count = -1;
if (site.getConfiguration().getLicenses() != null) {
for (License license : site.getConfiguration().getLicenses()) {
if (license.getUrl() == null) {
count++;
}
if (Integer.toString(count).equals(numberString)) {
ResponseBuilder rb = Response.ok(license2Representation(uriInfo.getAbsolutePath().toString(), license));
rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
}
}
} catch (NumberFormatException e) {
return Response.status(Status.NOT_FOUND).entity("No License found.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
}
}
return Response.status(Response.Status.NOT_FOUND).build();
}
use of javax.ws.rs.core.Response.ResponseBuilder in project stanbol by apache.
the class ReferencedSiteRootResource method executeLDPathQuery.
/**
* Execute a Query that uses LDPath to process results.
* @param query the query
* @param mediaType the mediaType for the response
* @param headers the http headers of the request
* @return the response
*/
private Response executeLDPathQuery(Site site, FieldQuery query, String ldpathProgramString, MediaType mediaType, HttpHeaders headers) {
QueryResultList<Representation> result;
ValueFactory vf = new RdfValueFactory(new IndexedGraph());
SiteBackend backend = new SiteBackend(site, vf);
EntityhubLDPath ldPath = new EntityhubLDPath(backend, vf);
//copy the selected fields, because we might need to delete some during
//the preparation phase
Set<String> selectedFields = new HashSet<String>(query.getSelectedFields());
//first prepare (only execute the query if the parameters are valid)
Program<Object> program;
try {
program = prepareQueryLDPathProgram(ldpathProgramString, selectedFields, backend, ldPath);
} catch (LDPathParseException e) {
log.warn("Unable to parse LDPath program used as select for Query:");
log.warn("FieldQuery: \n {}", query);
log.warn("LDPath: \n {}", ((LDPathSelect) query).getLDPathSelect());
log.warn("Exception:", e);
return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, mediaType).build();
} catch (IllegalStateException e) {
log.warn("parsed LDPath program is not compatible with parsed Query!", e);
return Response.status(Status.BAD_REQUEST).entity(e.getMessage()).header(HttpHeaders.ACCEPT, mediaType).build();
}
//2. execute the query
Iterator<Representation> resultIt;
try {
// we need to adapt from Entity to Representation
resultIt = new AdaptingIterator<Entity, Representation>(site.findEntities(query).iterator(), new AdaptingIterator.Adapter<Entity, Representation>() {
@Override
public Representation adapt(Entity value, Class<Representation> type) {
return value.getRepresentation();
}
}, Representation.class);
} catch (SiteException e) {
String message = String.format("Unable to Query Site '%s' (message: %s)", site.getId(), e.getMessage());
log.error(message, e);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message).header(HttpHeaders.ACCEPT, mediaType).build();
}
//process the results
Collection<Representation> transformedResults = transformQueryResults(resultIt, program, selectedFields, ldPath, backend, vf);
result = new QueryResultListImpl<Representation>(query, transformedResults, Representation.class);
ResponseBuilder rb = Response.ok(result);
rb.header(HttpHeaders.CONTENT_TYPE, mediaType + "; charset=utf-8");
//addCORSOrigin(servletContext, rb, headers);
return rb.build();
}
Aggregations