Search in sources :

Example 56 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jackrabbit-oak by apache.

the class PostBinaryHandler method handle.

@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {
    RemoteSession session = (RemoteSession) request.getAttribute("session");
    if (session == null) {
        sendInternalServerError(response, "session not found");
        return;
    }
    RemoteBinaryId binaryId = session.writeBinary(request.getInputStream());
    response.setStatus(HttpServletResponse.SC_CREATED);
    response.setContentType("application/json");
    ServletOutputStream stream = response.getOutputStream();
    JsonGenerator generator = new JsonFactory().createJsonGenerator(stream, JsonEncoding.UTF8);
    generator.writeStartObject();
    generator.writeStringField("binaryId", binaryId.asString());
    generator.writeEndObject();
    generator.flush();
    stream.close();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) RemoteSession(org.apache.jackrabbit.oak.remote.RemoteSession) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) RemoteBinaryId(org.apache.jackrabbit.oak.remote.RemoteBinaryId)

Example 57 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jena by apache.

the class UpdateValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        String[] args = httpRequest.getParameterValues(paramUpdate);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No update parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many update parameters");
            return;
        }
        final String updateString = httpRequest.getParameter(paramUpdate).replaceAll("(\r|\n| )*$", "");
        String updateSyntax = httpRequest.getParameter(paramSyntax);
        if (updateSyntax == null || updateSyntax.equals(""))
            updateSyntax = "SPARQL";
        Syntax language = Syntax.lookup(updateSyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + updateSyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        // Currently default.
        boolean outputSPARQL = true;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Update Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Update Validator</h1>");
        // Print as received
        {
            outStream.println("<p>Input:</p>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(updateString);
                }
            };
            output(outStream, c, lineNumbers);
        }
        // Attempt to parse it.
        UpdateRequest request = null;
        try {
            request = UpdateFactory.create(updateString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        // Because we pass into anon inner classes
        final UpdateRequest updateRequest = request;
        // OK?  Pretty print
        if (updateRequest != null && outputSPARQL) {
            outStream.println("<p>Formatted, parsed update request:</p>");
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    updateRequest.output(out);
                }
            };
            output(outStream, c, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) ServletOutputStream(javax.servlet.ServletOutputStream) UpdateRequest(org.apache.jena.update.UpdateRequest) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException) IOException(java.io.IOException)

Example 58 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jena by apache.

the class IRIValidator method execute.

@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        String[] args = httpRequest.getParameterValues(paramIRI);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        PrintStream stdout = System.out;
        PrintStream stderr = System.err;
        System.setOut(new PrintStream(outStream));
        System.setErr(new PrintStream(outStream));
        setHeaders(httpResponse);
        outStream.println("<html>");
        printHead(outStream, "Jena IRI Validator Report");
        outStream.println("<body>");
        outStream.println("<h1>IRI Report</h1>");
        startFixed(outStream);
        try {
            boolean first = true;
            for (String iriStr : args) {
                if (!first)
                    System.out.println();
                first = false;
                IRI iri = iriFactory.create(iriStr);
                System.out.println(iriStr + " ==> " + iri);
                if (iri.isRelative())
                    System.out.println("Relative IRI: " + iriStr);
                Iterator<Violation> vIter = iri.violations(true);
                for (; vIter.hasNext(); ) {
                    String str = vIter.next().getShortMessage();
                    str = htmlQuote(str);
                    System.out.println(str);
                }
            }
        } finally {
            finishFixed(outStream);
            System.out.flush();
            System.err.flush();
            System.setOut(stdout);
            System.setErr(stdout);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (IOException ex) {
    }
}
Also used : Violation(org.apache.jena.iri.Violation) PrintStream(java.io.PrintStream) IRI(org.apache.jena.iri.IRI) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException)

Example 59 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jena by apache.

the class QueryValidator method execute.

//static final String paramSyntaxExtended   = "extendedSyntax" ;
@Override
protected void execute(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        //            if ( log.isInfoEnabled() )
        //                log.info("validation request") ;
        String[] args = httpRequest.getParameterValues(paramQuery);
        if (args == null || args.length == 0) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "No query parameter to validator");
            return;
        }
        if (args.length > 1) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Too many query parameters");
            return;
        }
        final String queryString = httpRequest.getParameter(paramQuery).replaceAll("(\r|\n| )*$", "");
        //            queryString = queryString.replace("\r\n", "\n") ;
        //            queryString.replaceAll("(\r|\n| )*$", "") ;
        String querySyntax = httpRequest.getParameter(paramSyntax);
        if (querySyntax == null || querySyntax.equals(""))
            querySyntax = "SPARQL";
        Syntax language = Syntax.lookup(querySyntax);
        if (language == null) {
            httpResponse.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown syntax: " + querySyntax);
            return;
        }
        String lineNumbersArg = httpRequest.getParameter(paramLineNumbers);
        String[] a = httpRequest.getParameterValues(paramFormat);
        boolean outputSPARQL = false;
        boolean outputPrefix = false;
        boolean outputAlgebra = false;
        boolean outputQuads = false;
        boolean outputOptimized = false;
        boolean outputOptimizedQuads = false;
        if (a != null) {
            for (String anA : a) {
                if (anA.equals("sparql")) {
                    outputSPARQL = true;
                }
                if (anA.equals("prefix")) {
                    outputPrefix = true;
                }
                if (anA.equals("algebra")) {
                    outputAlgebra = true;
                }
                if (anA.equals("quads")) {
                    outputQuads = true;
                }
                if (anA.equals("opt")) {
                    outputOptimized = true;
                }
                if (anA.equals("optquads")) {
                    outputOptimizedQuads = true;
                }
            }
        }
        //            if ( ! outputSPARQL && ! outputPrefix )
        //                outputSPARQL = true ;
        boolean lineNumbers = true;
        if (lineNumbersArg != null)
            lineNumbers = lineNumbersArg.equalsIgnoreCase("true") || lineNumbersArg.equalsIgnoreCase("yes");
        // Headers
        setHeaders(httpResponse);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        outStream.println("<html>");
        printHead(outStream, "SPARQL Query Validation Report");
        outStream.println("<body>");
        outStream.println("<h1>SPARQL Query Validator</h1>");
        // Print query as received
        {
            outStream.println("<p>Input:</p>");
            // Not Java's finest hour.
            Content c = new Content() {

                @Override
                public void print(IndentedWriter out) {
                    out.print(queryString);
                }
            };
            output(outStream, c, lineNumbers);
        }
        // Attempt to parse it.
        Query query = null;
        try {
            query = QueryFactory.create(queryString, "http://example/base/", language);
        } catch (ARQException ex) {
            // Over generous exception (should be QueryException)
            // but this makes the code robust.
            outStream.println("<p>Syntax error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        } catch (RuntimeException ex) {
            outStream.println("<p>Internal error:</p>");
            startFixed(outStream);
            outStream.println(ex.getMessage());
            finishFixed(outStream);
        }
        if (query != null) {
            if (outputSPARQL)
                outputSyntax(outStream, query, lineNumbers);
            if (outputAlgebra)
                outputAlgebra(outStream, query, lineNumbers);
            if (outputQuads)
                outputAlgebraQuads(outStream, query, lineNumbers);
            if (outputOptimized)
                outputAlgebraOpt(outStream, query, lineNumbers);
            if (outputOptimizedQuads)
                outputAlgebraOptQuads(outStream, query, lineNumbers);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (Exception ex) {
        serviceLog.warn("Exception in doGet", ex);
    }
}
Also used : IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Query(org.apache.jena.query.Query) ServletOutputStream(javax.servlet.ServletOutputStream) ARQException(org.apache.jena.sparql.ARQException) Syntax(org.apache.jena.query.Syntax) ARQException(org.apache.jena.sparql.ARQException) IOException(java.io.IOException)

Example 60 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project jena by apache.

the class SPARQL_REST_R method doGet.

@Override
protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = HttpAction.contentNegotationRDF(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (action.verbose)
        log.info(format("[%d]   Get: Content-Type=%s, Charset=%s => %s", action.id, mediaType.getContentType(), mediaType.getCharset(), lang.getName()));
    action.beginRead();
    setCommonHeaders(action.response);
    try {
        Target target = determineTarget(action);
        if (log.isDebugEnabled())
            log.debug("GET->" + target);
        boolean exists = target.exists();
        if (!exists)
            errorNotFound("No such graph: <" + target.name + ">");
        // If we want to set the Content-Length, we need to buffer.
        //response.setContentLength(??) ;
        String ct = lang.getContentType().toHeaderString();
        action.response.setContentType(ct);
        Graph g = target.graph();
        //Special case RDF/XML to be the plain (faster, less readable) form
        RDFFormat fmt = (lang == Lang.RDFXML) ? RDFFormat.RDFXML_PLAIN : RDFWriterRegistry.defaultSerialization(lang);
        RDFDataMgr.write(out, g, fmt);
        success(action);
    } finally {
        action.endRead();
    }
}
Also used : Graph(org.apache.jena.graph.Graph) ServletOutputStream(javax.servlet.ServletOutputStream) MediaType(org.apache.jena.atlas.web.MediaType) TypedOutputStream(org.apache.jena.atlas.web.TypedOutputStream) IOException(java.io.IOException)

Aggregations

ServletOutputStream (javax.servlet.ServletOutputStream)509 IOException (java.io.IOException)212 HttpServletResponse (javax.servlet.http.HttpServletResponse)147 Test (org.junit.Test)112 HttpServletRequest (javax.servlet.http.HttpServletRequest)109 ServletException (javax.servlet.ServletException)91 InputStream (java.io.InputStream)62 File (java.io.File)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 FileInputStream (java.io.FileInputStream)40 CountDownLatch (java.util.concurrent.CountDownLatch)27 WriteListener (javax.servlet.WriteListener)27 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)27 PrintWriter (java.io.PrintWriter)26 HttpServlet (javax.servlet.http.HttpServlet)25 AsyncContext (javax.servlet.AsyncContext)23 ServletInputStream (javax.servlet.ServletInputStream)22 ArrayList (java.util.ArrayList)21 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)20 Date (java.util.Date)18