Example usage for org.apache.wicket.request.resource PartWriterCallback PartWriterCallback

List of usage examples for org.apache.wicket.request.resource PartWriterCallback PartWriterCallback

Introduction

In this page you can find the example usage for org.apache.wicket.request.resource PartWriterCallback PartWriterCallback.

Prototype

public PartWriterCallback(InputStream inputStream, Long contentLength, Long startbyte, Long endbyte) 

Source Link

Document

Creates a part writer callback.

Reads a part of the given input stream.

Usage

From source file:org.wicketstuff.nashorn.resource.NashornResource.java

License:Apache License

/**
 * Executes the java script code received from the client and returns the response
 *///from   w  w  w. j  a v  a2 s . c o m
@Override
protected ResourceResponse newResourceResponse(Attributes attributes) {
    RequestCycle cycle = RequestCycle.get();
    Long startbyte = cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
    Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
    HttpServletRequest httpServletRequest = (HttpServletRequest) attributes.getRequest().getContainerRequest();
    try (InputStream inputStream = httpServletRequest.getInputStream()) {
        String script = IOUtils.toString(inputStream);
        if (script.contains("nashornResourceReferenceScriptExecutionThread")) {
            throw new IllegalAccessException(
                    "It is not allowed to gain access to the nashorn thread itself! (nashornResourceReferenceScriptExecutionThread)");
        }
        String safeScript = ensureSafetyScript(script, attributes);
        NashornScriptCallable nashornScriptCallable = new NashornScriptCallable(safeScript, attributes,
                getClassFilter(), getWriter(), getErrorWriter()) {
            @Override
            protected void setup(Attributes attributes, Bindings bindings) {
                NashornResource.this.setup(attributes, bindings);
            }
        };
        Object scriptResult = executeScript(nashornScriptCallable, true);
        ResourceResponse resourceResponse = new ResourceResponse();
        resourceResponse.setContentType("text/plain");
        resourceResponse.setWriteCallback(new PartWriterCallback(
                IOUtils.toInputStream(scriptResult != null ? scriptResult.toString() : ""),
                Long.valueOf(scriptResult != null ? scriptResult.toString().length() : 0), startbyte, endbyte));
        return resourceResponse;
    } catch (Exception e) {
        ResourceResponse errorResourceResponse = processError(e);
        if (errorResourceResponse == null) {
            throw new WicketRuntimeException("Error while reading / executing the script the script", e);
        } else {
            return errorResourceResponse;
        }

    }
}