Search in sources :

Example 31 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class ReferencedSiteRootResource method findEntity.

@POST
@Path("/find")
public Response findEntity(@PathParam(value = "site") String siteId, @FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, // @FormParam(value="select") String select,
@FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
    Site site = getSite(siteId);
    log.debug("site/{}/find Request", site.getId());
    Collection<String> supported = new HashSet<String>(JerseyUtils.QUERY_RESULT_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
    if (name == null || name.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("find", new SiteResultData(site)));
            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("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    final String property;
    if (parsedField == null) {
        property = DEFAULT_FIND_FIELD;
    } else {
        parsedField = parsedField.trim();
        if (parsedField.isEmpty()) {
            property = DEFAULT_FIND_FIELD;
        } else {
            property = nsPrefixService.getFullName(parsedField);
            if (property == null) {
                String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
                return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
            }
        }
    }
    return executeQuery(site, createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath), headers);
}
Also used : ManagedSite(org.apache.stanbol.entityhub.servicesapi.site.ManagedSite) Site(org.apache.stanbol.entityhub.servicesapi.site.Site) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) POST(javax.ws.rs.POST)

Example 32 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class SiteManagerRootResource method findEntity.

@POST
@Path("/find")
public Response findEntity(@FormParam(value = "name") String name, @FormParam(value = "field") String parsedField, @FormParam(value = "lang") String language, // @FormParam(value="select") String select,
@FormParam(value = "limit") Integer limit, @FormParam(value = "offset") Integer offset, @FormParam(value = "ldpath") String ldpath, @Context HttpHeaders headers) {
    log.debug("findEntity() Request");
    Collection<String> supported = new HashSet<String>(JerseyUtils.QUERY_RESULT_SUPPORTED_MEDIA_TYPES);
    supported.add(TEXT_HTML);
    final MediaType acceptedMediaType = getAcceptableMediaType(headers, supported, MediaType.APPLICATION_JSON_TYPE);
    if (name == null || name.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            ResponseBuilder rb = Response.ok(new Viewable("find", 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("The name must not be null nor empty for find requests. Missing parameter name.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    final String property;
    if (parsedField == null) {
        property = DEFAULT_FIND_FIELD;
    } else {
        parsedField = parsedField.trim();
        if (parsedField.isEmpty()) {
            property = DEFAULT_FIND_FIELD;
        } else {
            property = nsPrefixService.getFullName(parsedField);
            if (property == null) {
                String messsage = String.format("The prefix '%s' of the parsed field '%' is not " + "mapped to any namespace. Please parse the full URI instead!\n", NamespaceMappingUtils.getPrefix(parsedField), parsedField);
                return Response.status(Status.BAD_REQUEST).entity(messsage).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
            }
        }
    }
    FieldQuery query = JerseyUtils.createFieldQueryForFindRequest(name, property, language, limit == null || limit < 1 ? DEFAULT_FIND_RESULT_LIMIT : limit, offset, ldpath);
    return executeQuery(referencedSiteManager, query, acceptedMediaType, headers);
}
Also used : FieldQuery(org.apache.stanbol.entityhub.servicesapi.query.FieldQuery) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) POST(javax.ws.rs.POST)

Example 33 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class SiteManagerRootResource method getReferencedSites.

// removed to allow request with Accept headers other than text/html to return
// the JSON array
//    @GET
//    @Path("/referenced")
//    @Produces(MediaType.TEXT_HTML)
//    public Response getReferencedSitesPage() {
//        return Response.ok(new Viewable("referenced", this))
//        .header(HttpHeaders.CONTENT_TYPE, TEXT_HTML+"; charset=utf-8").build();
//    }
/**
     * Getter for the id's of all referenced sites
     * 
     * @return the id's of all referenced sites.
     */
@GET
@Path(value = "/referenced")
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_HTML })
public Response getReferencedSites(@Context UriInfo uriInfo, @Context HttpHeaders headers) {
    MediaType acceptable = getAcceptableMediaType(headers, Arrays.asList(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML), MediaType.APPLICATION_JSON_TYPE);
    if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptable)) {
        ResponseBuilder rb = Response.ok(new Viewable("referenced", this));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else {
        JSONArray referencedSites = new JSONArray();
        for (String site : referencedSiteManager.getSiteIds()) {
            referencedSites.put(String.format("%sentityhub/site/%s/", uriInfo.getBaseUri(), site));
        }
        ResponseBuilder rb = Response.ok(referencedSites.toString());
        rb.header(HttpHeaders.CONTENT_TYPE, acceptable + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : Viewable(org.apache.stanbol.commons.web.viewable.Viewable) JSONArray(org.codehaus.jettison.json.JSONArray) MediaType(javax.ws.rs.core.MediaType) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) EntityhubLDPath(org.apache.stanbol.entityhub.ldpath.EntityhubLDPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 34 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class BaseGoogleRefineReconcileResource method query.

@GET
public final Response query(@PathParam(value = "site") String siteId, @QueryParam(value = "query") String query, @QueryParam(value = "queries") String queries, @QueryParam(value = "callback") String callback, @Context HttpHeaders header) throws WebApplicationException {
    if (callback != null) {
        log.info("callback: {}", callback);
        try {
            return sendMetadata(siteId, callback, header);
        } catch (JSONException e) {
            throw new WebApplicationException(e);
        }
    }
    JSONObject jResult;
    if (query != null) {
        log.debug("query: {}", query);
        try {
            jResult = reconcile(siteId, ReconcileQuery.parseQuery(query, nsPrefixService));
        } catch (JSONException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while writing Reconcilation results (%s: %s)", JSONException.class.getSimpleName(), e.getMessage())).build());
        } catch (EntityhubException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while searching on %s (%s: %s)", getSiteName(siteId), SiteException.class.getSimpleName(), e.getMessage())).build());
        }
    } else if (queries != null) {
        log.debug("multi-query: {}", queries);
        try {
            jResult = reconcile(siteId, ReconcileQuery.parseQueries(queries, nsPrefixService));
        } catch (JSONException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while writing Reconcilation results (%s: %s)", JSONException.class.getSimpleName(), e.getMessage())).build());
        } catch (EntityhubException e) {
            throw new WebApplicationException(Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(String.format("Error while searching on %s (%s: %s)", getSiteName(siteId), SiteException.class.getSimpleName(), e.getMessage())).build());
        }
    } else {
        if (MediaTypeUtil.isAcceptableMediaType(header, MediaType.TEXT_HTML_TYPE)) {
            ResponseBuilder rb = Response.ok(new Viewable("index", this, BaseGoogleRefineReconcileResource.class));
            rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
            //addCORSOrigin(servletContext, rb, header);
            return rb.build();
        }
        throw new WebApplicationException(Response.status(Response.Status.BAD_REQUEST).entity("One of the 'query' or 'querues' or 'callback=jsonp' parameter MUST BE present!").build());
    }
    //return the results and enable Cors
    ResponseBuilder rb = Response.ok(jResult.toString()).type(MediaType.APPLICATION_JSON_TYPE);
    //CorsHelper.addCORSOrigin(servletContext, rb, header);
    return rb.build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) JSONObject(org.codehaus.jettison.json.JSONObject) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) JSONException(org.codehaus.jettison.json.JSONException) EntityhubException(org.apache.stanbol.entityhub.servicesapi.EntityhubException) SiteException(org.apache.stanbol.entityhub.servicesapi.site.SiteException) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) GET(javax.ws.rs.GET)

Example 35 with Viewable

use of org.apache.stanbol.commons.web.viewable.Viewable in project stanbol by apache.

the class LDPathHelper method handleLDPathRequest.

/**
     * Processes LDPath requests as supported by the {@link SiteManagerRootResource},
     * {@link ReferencedSiteRootResource}, {@link EntityhubRootResource}.
     * @param resource The resource used as context when sending RESTful Service API
     * {@link Viewable} as response entity.
     * @param backend The {@link RDFBackend} implementation
     * @param ldpath the parsed LDPath program
     * @param contexts the Entities to execute the LDPath program
     * @param headers the parsed HTTP headers (used to determine the accepted
     * content type for the response
     * @param servletContext The Servlet context needed for CORS support
     * @return the Response {@link Status#BAD_REQUEST} or {@link Status#OK}.
     */
public static Response handleLDPathRequest(BaseStanbolResource resource, RDFBackend<Object> backend, String ldpath, Set<String> contexts, HttpHeaders headers) {
    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);
    boolean printDocu = false;
    //remove null and "" element
    contexts.remove(null);
    contexts.remove("");
    if (contexts == null || contexts.isEmpty()) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            printDocu = true;
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No context was provided by the Request. Missing parameter context.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    if (!printDocu & (ldpath == null || ldpath.isEmpty())) {
        if (MediaType.TEXT_HTML_TYPE.isCompatible(acceptedMediaType)) {
            printDocu = true;
        } else {
            return Response.status(Status.BAD_REQUEST).entity("No ldpath program was provided by the Request. Missing or empty parameter ldpath.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
        }
    }
    if (printDocu) {
        //a missing parameter and the content type is compatible to HTML
        ResponseBuilder rb = Response.ok(new Viewable("ldpath", resource));
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=utf-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    } else if (acceptedMediaType.equals(TEXT_HTML_TYPE)) {
        //HTML is only supported for documentation
        return Response.status(Status.NOT_ACCEPTABLE).entity("The requested content type " + TEXT_HTML + " is not supported.\n").header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    }
    Graph data;
    try {
        data = executeLDPath(backend, ldpath, contexts);
    } catch (LDPathParseException e) {
        log.warn("Unable to parse LDPath program:\n" + ldpath, e);
        return Response.status(Status.BAD_REQUEST).entity(("Unable to parse LDPath program (Messages: " + getLDPathParseExceptionMessage(e) + ")!\n")).header(HttpHeaders.ACCEPT, acceptedMediaType).build();
    }
    ResponseBuilder rb = Response.ok(data);
    rb.header(HttpHeaders.CONTENT_TYPE, acceptedMediaType + "; charset=utf-8");
    //addCORSOrigin(servletContext, rb, headers);
    return rb.build();
}
Also used : IndexedGraph(org.apache.stanbol.commons.indexedgraph.IndexedGraph) Graph(org.apache.clerezza.commons.rdf.Graph) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) MediaTypeUtil.getAcceptableMediaType(org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType) MediaType(javax.ws.rs.core.MediaType) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) LDPathParseException(org.apache.marmotta.ldpath.exception.LDPathParseException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

Viewable (org.apache.stanbol.commons.web.viewable.Viewable)43 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)40 GET (javax.ws.rs.GET)30 Produces (javax.ws.rs.Produces)26 Path (javax.ws.rs.Path)20 WebApplicationException (javax.ws.rs.WebApplicationException)15 EntityhubLDPath (org.apache.stanbol.entityhub.ldpath.EntityhubLDPath)14 MediaType (javax.ws.rs.core.MediaType)12 MediaTypeUtil.getAcceptableMediaType (org.apache.stanbol.commons.web.base.utils.MediaTypeUtil.getAcceptableMediaType)12 HashSet (java.util.HashSet)11 Entity (org.apache.stanbol.entityhub.servicesapi.model.Entity)7 POST (javax.ws.rs.POST)6 EntityhubException (org.apache.stanbol.entityhub.servicesapi.EntityhubException)6 IRI (org.semanticweb.owlapi.model.IRI)5 Consumes (javax.ws.rs.Consumes)4 OWLOntologyStorageException (org.semanticweb.owlapi.model.OWLOntologyStorageException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3