Search in sources :

Example 1 with EnhancementException

use of org.apache.stanbol.enhancer.servicesapi.EnhancementException in project stanbol by apache.

the class EventJobManagerImpl method enhanceContent.

@Override
public void enhanceContent(ContentItem ci, Chain chain) throws EnhancementException {
    if (ci == null) {
        throw new IllegalArgumentException("The parsed contentItem MUST NOT be NULL!");
    }
    if (chain == null) {
        throw new IllegalArgumentException("Unable to enhance ContentItem '" + ci.getUri() + "' because NULL was passed as enhancement chain");
    }
    long start = System.currentTimeMillis();
    enhancementJobManagerLog.debug(">> enhance {} with chain {}", ci.getUri(), chain.getName());
    boolean isDefaultChain = chain.equals(chainManager.getDefault());
    EnhancementJob job = new EnhancementJob(ci, chain.getName(), chain.getExecutionPlan(), isDefaultChain);
    //start the execution
    //wait for the results
    EnhancementJobObserver observer = jobHandler.register(job);
    //now wait for the execution to finish for the configured maximum time
    boolean completed = observer.waitForCompletion(maxEnhancementJobWaitTime);
    if (!completed) {
        //throw timeout exception
        StringBuilder sb = new StringBuilder("Status:\n");
        ExecutionMetadata em = ExecutionMetadata.parseFrom(job.getExecutionMetadata(), ci.getUri());
        for (Entry<String, Execution> ex : em.getEngineExecutions().entrySet()) {
            sb.append("  -").append(ex.getKey()).append(": ").append(ex.getValue().getStatus()).append('\n');
        }
        throw new ChainException("Execution timeout after " + ((System.currentTimeMillis() - start) / 1000f) + "sec (timeout:" + (maxEnhancementJobWaitTime / 1000) + "sec) for ContentItem " + ci.getUri() + "\n" + sb.toString() + " \n To change the timeout change value of property '" + MAX_ENHANCEMENT_JOB_WAIT_TIME + "' for the service " + getClass());
    }
    log.info("Execution of Chain {} {} after {}ms for ContentItem {}", new Object[] { chain.getName(), job.isFailed() ? "failed" : "finished", System.currentTimeMillis() - start, job.getContentItem().getUri() });
    //ci.getMetadata().addAll(job.getExecutionMetadata());
    if (job.isFailed()) {
        Exception e = job.getError();
        EnhancementJobHandler.logJobInfo(log, job, null, true);
        logExecutionMetadata(enhancementJobManagerLog, job, true);
        log.warn("ExecutionMetadata: ");
        for (Iterator<Triple> it = job.getExecutionMetadata().iterator(); it.hasNext(); log.warn(it.next().toString())) ;
        if (e instanceof SecurityException) {
            throw (SecurityException) e;
        } else if (e instanceof EnhancementException) {
            throw (EnhancementException) e;
        } else {
            throw new ChainException(job.getErrorMessage(), e);
        }
    }
    if (!job.isFinished()) {
        log.warn("Execution finished, but Job is not finished!");
        EnhancementJobHandler.logJobInfo(log, job, null, true);
        logExecutionMetadata(log, job, true);
        throw new ChainException("EnhancementJobManager was deactivated while" + " enhancing the passed ContentItem " + job.getContentItem() + " (EnhancementJobManager type: " + getClass() + ")");
    } else {
        //log infos about the execution times to the enhancementJobManager
        EnhancementJobHandler.logExecutionTimes(enhancementJobManagerLog, job);
        logExecutionMetadata(enhancementJobManagerLog, job, false);
    }
}
Also used : EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException) EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Triple(org.apache.clerezza.commons.rdf.Triple) ExecutionMetadata(org.apache.stanbol.enhancer.servicesapi.helper.execution.ExecutionMetadata) Execution(org.apache.stanbol.enhancer.servicesapi.helper.execution.Execution) EnhancementJobObserver(org.apache.stanbol.enhancer.jobmanager.event.impl.EnhancementJobHandler.EnhancementJobObserver) ChainException(org.apache.stanbol.enhancer.servicesapi.ChainException)

Example 2 with EnhancementException

use of org.apache.stanbol.enhancer.servicesapi.EnhancementException in project stanbol by apache.

the class GenericEnhancerUiResource method enhanceFromForm.

/**
     * Form-based OpenCalais-compatible interface
     * 
     * TODO: should we parse the OpenCalais paramsXML and find the closest Stanbol Enhancer semantics too?
     * 
     * Note: the format parameter is not part of the official API
     * 
     * @throws EngineException
     *             if the content is somehow corrupted
     * @throws IOException
     */
@POST
@Consumes(APPLICATION_FORM_URLENCODED)
public Response enhanceFromForm(@FormParam("content") String content, @FormParam("format") String format, @FormParam("ajax") boolean buildAjaxview, @Context HttpHeaders headers) throws EnhancementException, IOException {
    log.info("enhance from From: " + content);
    if (content == null) {
        //     content parameter
        throw new WebApplicationException(Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity("Parsing Content as 'application/x-www-form-urlencoded' is not supported!" + "Please directly POST the content and set the 'Content-Type' " + "header to the media type of the parsed content. 'application/" + "octet-stream' SHOULD BE used if the media type of the parsed " + "content is not known.\n").build());
    }
    ContentItem ci = ciFactory.createContentItem(new StringSource(content));
    if (!buildAjaxview) {
        //rewrite to a normal EnhancementRequest
        return enhanceFromData(ci, false, null, false, null, false, null, headers);
    } else {
        //enhance and build the AJAX response
        EnhancementException enhancementException;
        try {
            enhance(ci, null);
            enhancementException = null;
        } catch (EnhancementException e) {
            enhancementException = e;
        }
        ContentItemResource contentItemResource = new ContentItemResource(null, ci, getUriInfo(), "", serializer, getLayoutConfiguration(), enhancementException);
        contentItemResource.setRdfSerializationFormat(format);
        Viewable ajaxView = new Viewable("/ajax/contentitem", contentItemResource, ContentItemResource.class);
        ResponseBuilder rb = Response.ok(ajaxView);
        rb.header(HttpHeaders.CONTENT_TYPE, TEXT_HTML + "; charset=UTF-8");
        //addCORSOrigin(servletContext, rb, headers);
        return rb.build();
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) EnhancementException(org.apache.stanbol.enhancer.servicesapi.EnhancementException) Viewable(org.apache.stanbol.commons.web.viewable.Viewable) StringSource(org.apache.stanbol.enhancer.servicesapi.impl.StringSource) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ContentItem(org.apache.stanbol.enhancer.servicesapi.ContentItem) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

EnhancementException (org.apache.stanbol.enhancer.servicesapi.EnhancementException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)1 Triple (org.apache.clerezza.commons.rdf.Triple)1 Viewable (org.apache.stanbol.commons.web.viewable.Viewable)1 EnhancementJobObserver (org.apache.stanbol.enhancer.jobmanager.event.impl.EnhancementJobHandler.EnhancementJobObserver)1 ChainException (org.apache.stanbol.enhancer.servicesapi.ChainException)1 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)1 Execution (org.apache.stanbol.enhancer.servicesapi.helper.execution.Execution)1 ExecutionMetadata (org.apache.stanbol.enhancer.servicesapi.helper.execution.ExecutionMetadata)1 StringSource (org.apache.stanbol.enhancer.servicesapi.impl.StringSource)1