Example usage for javax.servlet.http HttpServletResponse isCommitted

List of usage examples for javax.servlet.http HttpServletResponse isCommitted

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse isCommitted.

Prototype

public boolean isCommitted();

Source Link

Document

Returns a boolean indicating if the response has been committed.

Usage

From source file:org.sakaiproject.nakamura.importer.ImportSiteArchiveServlet.java

private void sendError(int errorCode, String message, Throwable exception, HttpServletResponse response) {
    if (!response.isCommitted()) {
        try {//from ww  w  .  j a  v a  2  s .  c  o m
            LOG.error(errorCode + ": " + message, exception);
            response.sendError(errorCode, message);
        } catch (IOException e) {
            throw new Error(e);
        }
    } else {
        LOG.error(errorCode + ": " + message, exception);
        throw new Error(message, exception);
    }
}

From source file:com.cognitivabrasil.repositorio.web.FileControllerTest.java

@Test
public void testGetThumbnail() throws IOException {

    HttpServletResponse response = new MockHttpServletResponse();
    HttpServletResponse response2 = new MockHttpServletResponse();
    FileController fileController = mockFiles();

    // proves response and response2 are not comitted yet.
    Assert.assertFalse(response.isCommitted());
    Assert.assertFalse(response2.isCommitted());

    // tests id = null. 
    fileController.getThumbnail(null, response);
    Assert.assertTrue(response.isCommitted());
    assertThat(HttpServletResponse.SC_NOT_FOUND, equalTo(response.getStatus()));

    // tests id = 1      
    Long id = 1L;/* ww w  .  j a v  a 2  s  .c  o  m*/

    //proves response2 is only commited after flushbuffer.
    Assert.assertFalse(response2.isCommitted());
    fileController.getThumbnail(id, response2);
    Assert.assertTrue(response2.isCommitted());
    assertThat(HttpServletResponse.SC_CREATED, equalTo(response2.getStatus()));

}

From source file:com.alfaariss.oa.helper.stylesheet.StyleSheetEngine.java

private void sendDefault(HttpServletRequest oRequest, HttpServletResponse oResponse) {
    try {// w ww  .  j  av a  2  s  .  c om
        if (!oResponse.isCommitted()) {
            if (_wurflManager != null && _sDefaultMobileLocation != null && isWirelessDevice(oRequest))
                oResponse.sendRedirect(_sDefaultMobileLocation);
            else
                oResponse.sendRedirect(_sDefaultLocation);
        } else
            _logger.debug("Could not send default response");
    } catch (Exception e) {
        _logger.warn("Internal error during sending the default response", e);
    }
}

From source file:au.org.ala.biocache.web.AbstractSecureController.java

/**
  * Returns true when the operation should be performed.
  * @param apiKey//from  w  w  w . j a va 2 s  .  co  m
  * @param response
  * @return
  * @throws Exception
  */
public boolean shouldPerformOperation(String apiKey, HttpServletResponse response, boolean checkReadOnly)
        throws Exception {
    if (checkReadOnly && Store.isReadOnly()) {
        response.sendError(HttpServletResponse.SC_CONFLICT, "Server is in read only mode.  Try again later.");
    } else if (apiKey == null || !isValidKey(apiKey)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN, "An invalid API Key was provided.");
    }
    return !response.isCommitted();
}

From source file:com.cognitivabrasil.repositorio.web.FileControllerTest.java

@Test
public void testGetFile() throws IOException {

    com.cognitivabrasil.repositorio.data.entities.Files f3 = new Files();
    f3.setId(3);/*  w ww  . j  a  va 2 s  .c om*/
    f3.setName("testGet.txt");
    f3.setContentType("text");
    f3.setLocation("somewhere");

    HttpServletResponse response = new MockHttpServletResponse();
    HttpServletResponse response2 = new MockHttpServletResponse();
    FileController fileController = mockFiles();

    // proves response and response2 are not comitted yet.
    Assert.assertFalse(response.isCommitted());
    Assert.assertFalse(response2.isCommitted());

    int fileId = 3;
    when(fileService.get(fileId)).thenReturn(f3);

    // tests non-existent id.
    int id = 99;
    fileController.getFile(id, response);
    Assert.assertTrue(response.isCommitted());
    assertThat(HttpServletResponse.SC_GONE, equalTo(response.getStatus()));

    // tests valid id.  
    // Problem: how to make the getFile method avoid the f=null.
    id = 3;
    Assert.assertFalse(response2.isCommitted());
    fileController.getFile(id, response2);
    Assert.assertTrue(response2.isCommitted());
    assertThat(HttpServletResponse.SC_GONE, equalTo(response2.getStatus()));
    Assert.assertTrue(response2.isCommitted());

}

From source file:com.liferay.petra.doulos.servlet.DoulosServlet.java

protected void write(HttpServletResponse response, InputStream inputStream) throws IOException {

    OutputStream outputStream = null;

    try {/* w w  w  .j  a  v a  2s . c o  m*/
        response.setHeader("Cache-Control", "public");

        if (!response.isCommitted()) {
            outputStream = new BufferedOutputStream(response.getOutputStream());

            int c = inputStream.read();

            while (c != -1) {
                outputStream.write(c);

                c = inputStream.read();
            }
        }
    } finally {
        try {
            if (outputStream != null) {
                outputStream.flush();
            }
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e);
            }
        }

        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e);
            }
        }

        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            if (_log.isWarnEnabled()) {
                _log.warn(e);
            }
        }
    }
}

From source file:org.b3log.latke.servlet.renderer.AbstractFreeMarkerRenderer.java

/**
 * Processes the specified FreeMarker template with the specified request, data model and response.
 * <p>/* w  w w . j av  a2  s .  c om*/
 * Puts the page response contents into cache with the key getting from request attribute specified by <i>page cache
 * key</i>.
 * </p>
 *
 * @param html     the specified HTML content
 * @param request  the specified request
 * @param response the specified response
 * @throws Exception exception
 */
protected void doRender(final String html, final HttpServletRequest request, final HttpServletResponse response)
        throws Exception {
    PrintWriter writer;
    try {
        writer = response.getWriter();
    } catch (final Exception e) {
        writer = new PrintWriter(response.getOutputStream());
    }

    try {
        if (response.isCommitted()) { // response has been sent redirect
            writer.flush();
            writer.close();

            return;
        }

        writer.write(html);
        writer.flush();
        writer.close();
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Writes pipe failed: " + e.getMessage());
    }
}

From source file:info.magnolia.cms.filters.VirtualUriFilter.java

@Override
public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    final AggregationState aggregationState = MgnlContext.getAggregationState();
    String targetUri = getURIMapping(aggregationState.getCurrentURI(), aggregationState.getQueryString());

    if (StringUtils.isEmpty(targetUri)) {
        chain.doFilter(request, response);
        return;/*from   www  .  j ava2 s.c o m*/
    }

    if (response.isCommitted()) {
        log.warn("Response is already committed, cannot apply virtual URI {} (original URI was {})", targetUri,
                request.getRequestURI());
        chain.doFilter(request, response);
        return;
    }

    if (RequestDispatchUtil.dispatch(targetUri, request, response)) {
        return;
    }

    aggregationState.setCurrentURI(targetUri);
    chain.doFilter(request, response);
}

From source file:org.eclipse.orion.server.authentication.formopenid.servlets.LoginFormServlet.java

@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.service(req, resp);
    if (!resp.isCommitted()) {
        List<OpendIdProviderDescription> openidProviders;
        String customOpenids = req.getAttribute(OPENIDS_PROPERTY) == null ? getConfiguredOpenIds()
                : (String) req.getAttribute(OPENIDS_PROPERTY);
        if (customOpenids == null || customOpenids.trim().length() == 0) {
            openidProviders = getDefaultOpenIdProviders();
        } else {//from  w ww. j  a va  2  s. co m
            try {
                openidProviders = getSupportedOpenIdProviders(customOpenids);
            } catch (JSONException e) {
                LogHelper.log(new Status(IStatus.ERROR, Activator.PI_FORMOPENID_SERVLETS,
                        "Cannot load openid list, JSON format expected", e)); //$NON-NLS-1$
                openidProviders = getDefaultOpenIdProviders();
            }
        }

        // redirection from FormAuthenticationService.setNotAuthenticated
        String versionString = req.getHeader("Orion-Version"); //$NON-NLS-1$
        Version version = versionString == null ? null : new Version(versionString);

        // TODO: This is a workaround for calls
        // that does not include the WebEclipse version header
        String xRequestedWith = req.getHeader("X-Requested-With"); //$NON-NLS-1$

        if (version == null && !"XMLHttpRequest".equals(xRequestedWith)) { //$NON-NLS-1$
            writeHtmlResponse(req, resp, openidProviders);
        } else {
            writeJavaScriptResponse(req, resp, openidProviders);
        }
    }
}

From source file:info.magnolia.cms.servlets.Spool.java

/**
 * All static resource requests are handled here.
 *
 * @param request  HttpServletRequest//from w ww .jav  a 2s.c  o m
 * @param response HttpServletResponse
 * @throws IOException for error in accessing the resource or the servlet output stream
 */
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    File resource = new File(getServletContext().getRealPath(Path.getURI(request)));
    if (!resource.exists() || resource.isDirectory()) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    this.setResponseHeaders(resource, response);
    boolean success = this.spool(resource, response);
    if (!success && !response.isCommitted()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}