Package org.apache.stanbol.entityhub.servicesapi.site

Examples of org.apache.stanbol.entityhub.servicesapi.site.ManagedSite


                                          HttpHeaders headers) {
        long start = System.currentTimeMillis();
        MediaType accepted = getAcceptableMediaType(headers,
            JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES,
            MediaType.APPLICATION_JSON_TYPE);
        ManagedSite managedSite;
        if(site instanceof ManagedSite){
            managedSite = (ManagedSite)site;
        } else {
            ResponseBuilder builder =  Response.status(Status.FORBIDDEN).entity(
                String.format("The Site '%s' is not managed and does not support "
                    +"create/update nor delete operations",site.getId()))
            .header(HttpHeaders.ACCEPT, accepted);
            addCORSOrigin(servletContext, builder, headers);
            return builder.build();
        }
        //(1) if an id is parsed we need to ignore all other representations
        if(id != null && !"*".equals(id)){
            Representation r = parsed.get(id);
            if(r == null){
                ResponseBuilder builder = Response.status(Status.BAD_REQUEST)
                .entity(String.format("Parsed RDF data do not contain any "
                    + "Information about the parsed id '%s'",id))
                    .header(HttpHeaders.ACCEPT, accepted);
                addCORSOrigin(servletContext, builder, headers);
                return builder.build();
            } else {
                parsed = Collections.singletonMap(id, r);
            }
        }
        //First check if all parsed Representation can be created/updated
        if(!(create && update)){ //if both create and update are enabled skip this
            log.debug("   ... validate parsed Representation state (create: {}| update: {})",
                create,update);
            for(Entry<String,Representation> entry : parsed.entrySet()){
                boolean exists;
                try {
                    exists = managedSite.getEntity(entry.getKey()) != null;
                } catch (SiteException e) {
                    log.error(String.format("Exception while checking the existance " +
                        "of an Entity with id  %s in the Entityhub.",
                        entry.getKey()),e);
                    ResponseBuilder builder =  Response.status(Status.INTERNAL_SERVER_ERROR)
                    .entity(String.format("Unable to process Entity %s because of" +
                            "an Error while checking the current version of that" +
                            "Entity within the Entityhub (Message: %s)",
                            entry.getKey(),e.getMessage()))
                            .header(HttpHeaders.ACCEPT, accepted);
                    addCORSOrigin(servletContext, builder, headers);
                    return builder.build();
                }
                if((exists && !update) || (!exists && !create)){
                    ResponseBuilder builder = Response.status(Status.BAD_REQUEST).entity(String.format(
                        "Unable to %s an Entity %s becuase it %s and %s is deactivated. " +
                        " You might want to set the '%s' parameter to TRUE in your Request",
                        exists ? "update" : "create", entry.getKey(),
                        exists ? "does already exists " : "does not",
                        exists ? "updateing existing" : "creating new",
                        exists ? "does already" : "does not exists",
                        exists ? "update" : "create"))
                        .header(HttpHeaders.ACCEPT, accepted);
                    addCORSOrigin(servletContext, builder, headers);
                    return builder.build();

                }
            }
        }
        long validateCompleted = System.currentTimeMillis();
        log.info("   ... validate request data {}ms",
            validateCompleted-start);
        try {
            managedSite.store(parsed.values());
        } catch (ManagedSiteException e) {
            log.error(String.format("Exception while storing parsed Representations "
                + "in the ManagedSite %s",managedSite.getId()),e);
            ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR)
            .entity("Unable to store parsed Entities to ManagedSite "
                + managedSite.getId() +" because of an error (Message: "
                + e.getMessage()+")")
            .header(HttpHeaders.ACCEPT, accepted);
            addCORSOrigin(servletContext, builder, headers);
            return builder.build();
        }
View Full Code Here


    public Response deleteEntity(@QueryParam(value="id") String id,
                                 @Context HttpHeaders headers){
        MediaType accepted = getAcceptableMediaType(headers,
            JerseyUtils.ENTITY_SUPPORTED_MEDIA_TYPES,
            MediaType.APPLICATION_JSON_TYPE);
        ManagedSite managedSite;
        if(site instanceof ManagedSite){
            managedSite = (ManagedSite)site;
        } else {
            ResponseBuilder builder =  Response.status(Status.FORBIDDEN).entity(
                String.format("The Site '%s' is not managed and does not support "
                    +"create/update nor delete operations",site.getId()))
            .header(HttpHeaders.ACCEPT, accepted);
            addCORSOrigin(servletContext, builder, headers);
            return builder.build();
        }
        if(id == null || id.isEmpty()){
            ResponseBuilder builder =  Response.status(Status.BAD_REQUEST).entity("The Request does" +
                    "not provide the id of the Entity to delete (parameter 'id').")
                    .header(HttpHeaders.ACCEPT, accepted);
            addCORSOrigin(servletContext, builder, headers);
            return builder.build();
        }
        ResponseBuilder builder;
        try {
            if("*".equals(id)){
                managedSite.deleteAll();
                builder = Response.noContent();
            } else if(managedSite.getEntity(id) != null){
                managedSite.delete(id);
                builder = Response.noContent();
            } else {
                builder = Response.status(Status.NOT_FOUND).entity(
                    "No Entity with the parsed Id '"+id+"' is present on the ManagedSite '"
                    + managedSite.getId()+"'!")
                .header(HttpHeaders.ACCEPT, accepted);
            }
        } catch (SiteException e) {
            String message = "Exception while deleting '"+id+"' from ManagedSite '"
                    + managedSite.getId()+"'!";
            log.error(message,e);
            builder = Response.status(Status.INTERNAL_SERVER_ERROR)
            .entity(message + ' '+ e.getClass().getSimpleName()+": "+ e.getMessage())
            .header(HttpHeaders.ACCEPT, accepted);
        }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.entityhub.servicesapi.site.ManagedSite

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.