Search in sources :

Example 66 with ServletOutputStream

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

the class SPARQL_GSP_R method doGet.

@Override
protected void doGet(HttpAction action) {
    // Assume success - do the set up before grabbing the lock.
    // Sets content type.
    MediaType mediaType = ActionLib.contentNegotationRDF(action);
    ServletOutputStream output;
    try {
        output = action.response.getOutputStream();
    } catch (IOException ex) {
        ServletOps.errorOccurred(ex);
        output = null;
    }
    TypedOutputStream out = new TypedOutputStream(output, mediaType);
    Lang lang = RDFLanguages.contentTypeToLang(mediaType.getContentType());
    if (action.verbose)
        action.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 (action.log.isDebugEnabled())
            action.log.debug("GET->" + target);
        boolean exists = target.exists();
        if (!exists)
            ServletOps.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);
        try {
            RDFDataMgr.write(out, g, fmt);
        } catch (JenaException ex) {
            // Good news - this happens before any output for RDF/XML-ABBREV. 
            if (fmt.getLang().equals(Lang.RDFXML))
                ServletOps.errorBadRequest("Failed to write output in RDF/XML: " + ex.getMessage());
            else
                ServletOps.errorOccurred("Failed to write output: " + ex.getMessage(), ex);
        }
        ServletOps.success(action);
    } finally {
        action.endRead();
    }
}
Also used : JenaException(org.apache.jena.shared.JenaException) 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)

Example 67 with ServletOutputStream

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

the class ActionStats method statsTxt.

private void statsTxt(HttpServletResponse resp, DataAccessPointRegistry registry) throws IOException {
    ServletOutputStream out = resp.getOutputStream();
    resp.setContentType(contentTypeTextPlain);
    resp.setCharacterEncoding(charsetUTF8);
    Iterator<String> iter = registry.keys().iterator();
    while (iter.hasNext()) {
        String ds = iter.next();
        DataAccessPoint desc = registry.get(ds);
        statsTxt(out, desc);
        if (iter.hasNext())
            out.println();
    }
    out.flush();
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 68 with ServletOutputStream

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

the class RequestLoggerResponse method getOutputStream.

// ---------- SlingHttpServletResponse interface
@Override
public ServletOutputStream getOutputStream() throws IOException {
    if (this.out == null) {
        ServletOutputStream sos = super.getOutputStream();
        this.out = new LoggerResponseOutputStream(sos);
    }
    return this.out;
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 69 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project spring-boot by spring-projects.

the class ExampleServlet method service.

@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    String content = "Hello World";
    if (this.echoRequestInfo) {
        content += " scheme=" + request.getScheme();
        content += " remoteaddr=" + request.getRemoteAddr();
    }
    if (this.writeWithoutContentLength) {
        response.setContentType("text/plain");
        ServletOutputStream outputStream = response.getOutputStream();
        StreamUtils.copy(content.getBytes(), outputStream);
        outputStream.flush();
    } else {
        response.getWriter().write(content);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream)

Example 70 with ServletOutputStream

use of javax.servlet.ServletOutputStream in project solo by b3log.

the class CaptchaProcessorTestCase method get.

/**
     * get.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void get() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/captcha.do");
    when(request.getMethod()).thenReturn("GET");
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getOutputStream()).thenReturn(new ServletOutputStream() {

        private long size;

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setWriteListener(final WriteListener writeListener) {
        }

        @Override
        public void write(int b) throws IOException {
            size++;
        }

        @Override
        public String toString() {
            return "size: " + String.valueOf(size);
        }
    });
    dispatcherServlet.service(request, response);
    Assert.assertTrue(StringUtils.startsWith(response.getOutputStream().toString(), "size: "));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletOutputStream(javax.servlet.ServletOutputStream) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) WriteListener(javax.servlet.WriteListener) Test(org.testng.annotations.Test)

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