Example usage for javax.servlet.http HttpServletResponse SC_NO_CONTENT

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

Introduction

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

Prototype

int SC_NO_CONTENT

To view the source code for javax.servlet.http HttpServletResponse SC_NO_CONTENT.

Click Source Link

Document

Status code (204) indicating that the request succeeded but that there was no new information to return.

Usage

From source file:org.mule.transport.servlet.ServletResponseWriter.java

/**
 * Writes an empty {@link javax.servlet.http.HttpServletResponse}.
 *
 * @param servletResponse response object
 * @param httpHeaders headers to be set in the response object. Can be null.
 * @throws IOException//from w  w w .j a  v  a 2s  . c o m
 */
public void writeEmptyResponse(HttpServletResponse servletResponse, Map<String, String> httpHeaders)
        throws IOException {
    addHeaders(servletResponse, httpHeaders);
    servletResponse.setStatus(HttpServletResponse.SC_NO_CONTENT);
    if (feedbackOnEmptyResponse) {
        servletResponse.setStatus(HttpServletResponse.SC_OK);
        servletResponse.getWriter().write("Action was processed successfully. There was no result");
    }
    servletResponse.flushBuffer();
}

From source file:net.sf.ehcache.constructs.web.ResponseUtil.java

/**
 * Performs a number of checks to ensure response saneness according to the rules of RFC2616:
 * <ol>/*w w  w. j a v a2s.  com*/
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NO_CONTENT} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5
 * <li>If the response code is {@link javax.servlet.http.HttpServletResponse#SC_NOT_MODIFIED} then it is illegal for the body
 * to contain anything. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.5
 * </ol>
 *
 * @param request         the client HTTP request
 * @param responseStatus         the responseStatus
 * @return true if the response should be 0, even if it is isn't.
 */
public static boolean shouldBodyBeZero(HttpServletRequest request, int responseStatus) {

    //Check for NO_CONTENT
    if (responseStatus == HttpServletResponse.SC_NO_CONTENT) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NO_CONTENT
                    + " response. Removing message body in accordance with RFC2616.");
        }
        return true;
    }

    //Check for NOT_MODIFIED
    if (responseStatus == HttpServletResponse.SC_NOT_MODIFIED) {
        if (LOG.isDebugEnabled()) {
            LOG.debug(request.getRequestURL() + " resulted in a " + HttpServletResponse.SC_NOT_MODIFIED
                    + " response. Removing message body in accordance with RFC2616.");
        }
        return true;
    }

    return false;
}

From source file:com.thinkberg.moxo.dav.CopyMoveBase.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    boolean overwrite = getOverwrite(request);
    FileObject object = getResourceManager().getFileObject(request.getPathInfo());
    FileObject targetObject = getDestination(request);

    try {/* w  ww  . j  av a  2 s.c om*/
        // check that we can write the target
        LockManager.getInstance().checkCondition(targetObject, getIf(request));
        // if we move, check that we can actually write on the source
        if ("MOVE".equals(request.getMethod())) {
            LockManager.getInstance().checkCondition(object, getIf(request));
        }
    } catch (LockException e) {
        if (e.getLocks() != null) {
            response.sendError(SC_LOCKED);
        } else {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
        return;
    }

    if (null == targetObject) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }

    if (object.equals(targetObject)) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (targetObject.exists()) {
        if (!overwrite) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    } else {
        FileObject targetParent = targetObject.getParent();
        if (!targetParent.exists() || !FileType.FOLDER.equals(targetParent.getType())) {
            response.sendError(HttpServletResponse.SC_CONFLICT);
        }
        response.setStatus(HttpServletResponse.SC_CREATED);
    }

    copyOrMove(object, targetObject, getDepth(request));
}

From source file:com.xpn.xwiki.web.SaveAndContinueAction.java

/**
 * {@inheritDoc}//www. j a  v a 2  s  .c o  m
 * 
 * @see XWikiAction#action(XWikiContext)
 */
@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }

    // Try to find the URL of the edit page which we came from
    String back = findBackURL(context);

    // This will never be true if "back" comes from request.getHeader("referer")
    if (back != null && back.contains("editor=class")) {
        PropUpdateAction pua = new PropUpdateAction();
        if (pua.propUpdate(context)) {
            pua.render(context);
        }
    } else {
        SaveAction sa = new SaveAction();
        if (sa.save(context)) {
            sa.render(context);
        } else {
            // Lock back the document
            context.getDoc().getTranslatedDocument(context).setLock(context.getUser(), context);
        }
    }

    // If this is an ajax request, no need to redirect.
    if (Utils.isAjaxRequest(context)) {
        context.getResponse().setStatus(HttpServletResponse.SC_NO_CONTENT);
        return false;
    }

    // Forward back to the originating page
    try {
        context.getResponse().sendRedirect(back);
    } catch (IOException ignored) {
        // This exception is ignored because it will only be thrown if content has already been sent to the
        // response. This should never happen but we have to catch the exception anyway.
    }
    return false;
}

From source file:org.appcelerator.transport.DownloadTransportServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String ticket = request.getParameter("ticket");
    if (ticket == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "ticket required to use this service");
        return;//  w  w  w .j  av  a 2s.c o m
    }
    String name = request.getParameter("name");
    if (name == null) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "name required to use this service");
        return;
    }

    boolean handled = ServiceRegistry.dispatch(request, ticket, name, response);

    if (!handled) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    }
}

From source file:de.unirostock.sems.cbarchive.web.servlet.DownloadServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // set charset
    response.setCharacterEncoding(Fields.CHARSET);
    request.setCharacterEncoding(Fields.CHARSET);

    // login stuff
    UserManager user = null;/*  www.  j  ava 2  s  . c om*/
    try {
        user = Tools.doLogin(request, response, false);
    } catch (CombineArchiveWebCriticalException e) {
        LOGGER.error(e, "Exception while getting User");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    } catch (CombineArchiveWebException e) {
        LOGGER.warn(e, "Exception while getting User");
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        return;
    }

    // splitting request URL
    String[] requestUrl = request.getRequestURI().substring(request.getContextPath().length()).split("/");

    // check entry points
    if (requestUrl.length >= 5 && requestUrl[2].equals("archive")) {
        // request to download an archive from *any* workspace
        // without necessarily obtained this workspace before

        UserManager targetUser = null;
        if (requestUrl[3] != null && !requestUrl[3].isEmpty())
            targetUser = new UserManager(requestUrl[3]);
        else
            return;

        if (requestUrl[4] != null && !requestUrl[4].isEmpty() && targetUser != null)
            downloadArchive(request, response, targetUser, URLDecoder.decode(requestUrl[4], Fields.CHARSET));
    } else if (requestUrl.length >= 4 && requestUrl[2].equals("archive")) {
        // request to download an archive from the workspace
        if (requestUrl[3] != null && !requestUrl[3].isEmpty())
            downloadArchive(request, response, user, URLDecoder.decode(requestUrl[3], Fields.CHARSET));
    } else if (requestUrl.length >= 5 && requestUrl[2].equals("file")) {

        String archive = null;
        String file = null;

        if (requestUrl[3] != null && !requestUrl[3].isEmpty())
            archive = URLDecoder.decode(requestUrl[3], Fields.CHARSET);
        else
            return;

        StringBuilder filePath = new StringBuilder();
        for (int i = 4; i < requestUrl.length; i++) {

            if (requestUrl[i] != null && !requestUrl[i].isEmpty()) {
                filePath.append("/");
                filePath.append(requestUrl[i]);
            }
        }
        // decode the name
        file = URLDecoder.decode(filePath.toString(), Fields.CHARSET);

        if (archive != null && !archive.isEmpty() && file != null && !file.isEmpty())
            downloadFile(request, response, user, archive, file);

    }

}

From source file:de.mpg.escidoc.services.pidcache.web.MainServlet.java

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    logger.info("PID cache GET request");

    try {/*from   w  w w  . j  av  a 2 s.  co  m*/

        if (!authenticate(req, resp)) {
            logger.warn("Unauthorized request from " + req.getRemoteHost());
            return;
        }

        PidCacheService pidCacheService = new PidCacheService();

        if (GwdgPidService.GWDG_PIDSERVICE_VIEW.equals(req.getPathInfo())
                || GwdgPidService.GWDG_PIDSERVICE_VIEW.concat("/").equals(req.getPathInfo())) {
            if (req.getParameter("pid") == null) {
                resp.sendError(HttpServletResponse.SC_NO_CONTENT, "PID parameter failed.");
            }
            resp.getWriter().append(pidCacheService.retrieve(req.getParameter("pid")));
        } else if ("/cache/size".equals(req.getPathInfo())) {
            resp.getWriter().append("There are " + pidCacheService.getCacheSize() + " PID stored in cache");
        } else if ("/queue/size".equals(req.getPathInfo())) {
            resp.getWriter().append("There are " + pidCacheService.getQueueSize() + " PID actually in queue");
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, req.getPathInfo());
        }
    } catch (Exception e) {
        throw new ServletException("Error processing request", e);
    }
}

From source file:org.apereo.openlrs.OpenLRSAuthenticationFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    if (!enabled) {
        log.warn("Authentication is disabled");
        filterChain.doFilter(request, response);
    } else {/*from   w w  w .  j a  v  a  2 s .  c o m*/
        String authorizationHeader = request.getHeader("Authorization");

        if (log.isDebugEnabled()) {
            log.debug(String.format("Authorization Header: %s", authorizationHeader));
        }

        if (StringUtils.isNotBlank(authorizationHeader)) {
            if (StringUtils.containsIgnoreCase(authorizationHeader, "oauth")) {
                authenticateOAuth(authorizationHeader, request, response, filterChain);
            } else {
                authenticateBasic(authorizationHeader, request, response, filterChain);
            }
        } else if ("OPTIONS".equals(request.getMethod())) {
            log.warn("OPTIONS request - returning no content");
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        } else {
            unauthorized(response, "Missing Authorization Header", "None");
        }
    }
}

From source file:de.mpg.mpdl.inge.pidcache.web.MainServlet.java

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    logger.info("PID cache GET request");

    try {/*from  w  w  w  .  ja v a  2 s  .  co  m*/

        if (!authenticate(req, resp)) {
            logger.warn("Unauthorized request from " + req.getRemoteHost());
            return;
        }

        PidCacheService pidCacheService = new PidCacheService();

        if (GwdgPidService.GWDG_PIDSERVICE_VIEW.equals(req.getPathInfo())
                || GwdgPidService.GWDG_PIDSERVICE_VIEW.concat("/").equals(req.getPathInfo())) {
            if (req.getParameter("pid") == null) {
                resp.sendError(HttpServletResponse.SC_NO_CONTENT, "PID parameter failed.");
            }
            resp.getWriter().append(pidCacheService.retrieve(req.getParameter("pid")));
        } else if (GwdgPidService.GWDG_PIDSERVICE_FIND.equals(req.getPathInfo())
                || GwdgPidService.GWDG_PIDSERVICE_FIND.concat("/").equals(req.getPathInfo())) {
            if (req.getParameter("url") == null) {
                resp.sendError(HttpServletResponse.SC_NO_CONTENT, "URL parameter failed.");
            }
            resp.getWriter().append(pidCacheService.search(req.getParameter("url")));
        } else if ("/cache/size".equals(req.getPathInfo())) {
            resp.getWriter().append("There are " + pidCacheService.getCacheSize() + " PID stored in cache");
        } else if ("/queue/size".equals(req.getPathInfo())) {
            resp.getWriter().append("There are " + pidCacheService.getQueueSize() + " PID actually in queue");
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, req.getPathInfo());
        }
    } catch (Exception e) {
        throw new ServletException("Error processing request", e);
    }
}

From source file:com.xpn.xwiki.web.ObjectRemoveAction.java

/**
 * {@inheritDoc}/*from  w ww.  ja v a2 s . co  m*/
 * 
 * @see XWikiAction#action(XWikiContext)
 */
@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }

    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    String username = context.getUser();
    XWikiDocument doc = context.getDoc();
    BaseObject obj = getObject(doc, context);
    if (obj == null) {
        return true;
    }

    doc.removeObject(obj);
    doc.setAuthor(username);
    xwiki.saveDocument(doc, context.getMessageTool().get("core.comment.deleteObject"), true, context);

    if (Utils.isAjaxRequest(context)) {
        response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        response.setContentLength(0);
    } else {
        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
    }
    return false;
}