Example usage for javax.servlet.http HttpServletResponse SC_SERVICE_UNAVAILABLE

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

Introduction

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

Prototype

int SC_SERVICE_UNAVAILABLE

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

Click Source Link

Document

Status code (503) indicating that the HTTP server is temporarily overloaded, and unable to handle the request.

Usage

From source file:com.webtide.jetty.load.generator.web.MonitorServlet.java

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

    if (_restrictToLocalhost) {
        if (!isLoopbackAddress(req.getRemoteAddr())) {
            resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }/*w  ww. j  av a  2 s .  c o m*/
    }

    if (Boolean.valueOf(req.getParameter("start"))) {
        start = platformMonitor.start();
        return;
    }

    if (Boolean.valueOf(req.getParameter("stop"))) {
        stop = platformMonitor.stop();
        return;
    }

    if (Boolean.valueOf(req.getParameter("stats"))) {
        if (start == null) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }

        if (stop == null) {
            stop = platformMonitor.stop();
        }
        sendResponse(resp);
    }
}

From source file:com.kappaware.logtrawler.output.flow.PostJsonOutputFlow.java

@Override
protected void doOutput(Object obj) {
    if (this.httpClient == null) {
        this.httpClient = new HttpClient(SslHandling.NONE, On404.ERROR);
    }//from w w w. ja v a 2 s.  com
    boolean success;
    do {
        try {
            String r = this.httpClient.post(this.endpoint, this.contentType, Json.toJson(obj, false), null);
            log.debug(String.format("Return from '%s' : '%s'", this.endpoint, r));
            success = true;
        } catch (HttpClientException e) {
            if (e.getStatusCode() == null || e.getStatusCode() == HttpServletResponse.SC_SERVICE_UNAVAILABLE) {
                // Server does not respond, or is temporary Unavailable   
                log.error(String.format("Unable to connect to '%s'. Will retry", this.endpoint), e);
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e1) {
                }
                success = false;
            } else {
                log.error(String.format("Error connecting to '%s'. Message dropped", this.endpoint), e);
                success = true;
            }
        }
    } while (!success);
}

From source file:com.thoughtworks.go.server.newsecurity.filters.helpers.ServerUnavailabilityResponse.java

public void render() {
    response.setHeader("Cache-Control", "private, max-age=0, no-cache");
    response.setDateHeader("Expires", 0);

    if (isAPIRequest(request)) {
        generateAPIResponse();//from  w w  w .  j  av  a2 s . c  om
    } else {
        generateHTMLResponse();
    }

    response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}

From source file:edu.mayo.cts2.framework.webapp.rest.osgi.ProxyServlet.java

@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    HttpServlet dispatcher = (HttpServlet) this.tracker.getService();
    if (dispatcher != null) {
        dispatcher.service(req, res);/* w w w .  j a v  a 2  s.c  o m*/
    } else {
        res.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    }
}

From source file:och.comp.web.BaseErrorsServlet.java

public static void set503Status(HttpServletRequest req, HttpServletResponse resp) {
    setStatus(req, resp, HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}

From source file:nl.armatiek.xslweb.web.filter.WebAppFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;
    String path = StringUtils.defaultString(req.getPathInfo()) + req.getServletPath();
    WebApp webApp = getWebApp(request);// w  w w .  j av  a 2 s  . c  o m
    if (webApp == null) {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    } else if (webApp.isClosed()) {
        resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    } else {
        req.setAttribute(Definitions.ATTRNAME_WEBAPP, webApp);
        Resource resource = webApp.matchesResource(webApp.getRelativePath(path));
        if (resource == null) {
            chain.doFilter(request, response);
        } else {
            resp.setContentType(resource.getMediaType());
            File file = webApp.getStaticFile(path);
            if (!file.isFile()) {
                resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            } else {
                Date currentDate = new Date();
                long now = currentDate.getTime();
                long duration = resource.getDuration().getTimeInMillis(currentDate);
                resp.addHeader("Cache-Control", "max-age=" + duration / 1000);
                resp.setDateHeader("Expires", now + duration);
                FileUtils.copyFile(file, resp.getOutputStream());
            }
        }
    }
}

From source file:nl.ctrlaltdev.harbinger.filter.HttpEvidenceFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
        throws ServletException, IOException {
    Evidence evidence = new Evidence(request);
    if (isValid(request, evidence)) {
        try {/*from w  w  w.  j  a  va 2s.  co  m*/
            chain.doFilter(request, response);
        } catch (IOException | ServletException | RuntimeException ex) {
            evidence = new Evidence(evidence, ex);
            throw ex;
        } finally {
            Evidence ev = ctx.getEvidenceCollector().store(new Evidence(evidence, response));
            ctx.getResponseDecider().decide(ev).perform(ctx);
        }
    } else {
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    }
}

From source file:org.cloudfoundry.identity.uaa.web.RecognizeFailureDispatcherServlet.java

@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    if (failed) {
        String msg = "FAILURE";
        HttpServletResponse response = (HttpServletResponse) res;
        response.addHeader(HEADER, HEADER_MSG);
        response.setContentType(MediaType.TEXT_PLAIN_VALUE);
        response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        response.getWriter().write(msg);
        response.getWriter().flush();/*from  ww w . j a v a2 s  .co  m*/
    } else {
        delegate.service(req, res);
    }
}

From source file:org.pentaho.platform.web.servlet.GetImage.java

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    try {//w  ww  . j av a2  s  .c o m
        PentahoSystem.systemEntryPoint();

        final String image = request.getParameter("image"); //$NON-NLS-1$
        if (image != null && !"".equals(image)) {
            if (ServletBase.debug) {
                debug(Messages.getInstance().getString("IMAGE.DEBUG_IMAGE_PARAMETER") + image); //$NON-NLS-1$
            }
        } else {
            error(Messages.getInstance().getErrorString("IMAGE.ERROR_0001_IMAGE_PARAMETER_EMPTY")); //$NON-NLS-1$
            response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }

        // some sanity checks ...
        if (StringUtil.doesPathContainParentPathSegment(image)) {
            error(Messages.getInstance().getErrorString("IMAGE.ERROR_0002_FILE_NOT_FOUND", image)); //$NON-NLS-1$
            // we don't give hints that we check the parameter. Just return not
            // found.
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        String location = ""; //$NON-NLS-1$
        if (image.startsWith("/") || image.startsWith("\\")) { //$NON-NLS-1$ //$NON-NLS-2$
            location = "system/tmp/" + image.substring(1); //$NON-NLS-1$ 
        } else if (image.startsWith("tmp/") || image.startsWith("tmp\\")) { //$NON-NLS-1$ //$NON-NLS-2$
            location = "system/" + image; //$NON-NLS-1$
        } else {
            location = "system/tmp/" + image; //$NON-NLS-1$
        }

        File tmpFile = new File(PentahoSystem.getApplicationContext().getSolutionPath(location));
        // if (image.charAt(0) != '/' && image.charAt(0) != '\\') {
        // file = new File(tempDirectory, image);
        // } else {
        // file = new File(tempDirectory, image.substring(1));
        // }

        // paranoia: Check whether the new file is contained in the temp
        // directory.
        // an evil user could simply use "//" as parameter and would therefore
        // circument the test above ...
        // IOUtils ioUtils = IOUtils.getInstance();
        // if (ioUtils.isSubDirectory(tempDirectory, file) == false) {
        //        error(Messages.getInstance().getErrorString("IMAGE.ERROR_0002_FILE_NOT_FOUND", image)); //$NON-NLS-1$
        // // we dont give hints that we check the parameter. Just return not
        // // found.
        // response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        // return;
        // }

        if (!tmpFile.exists()) {
            error(Messages.getInstance().getErrorString("IMAGE.ERROR_0002_FILE_NOT_FOUND", image)); //$NON-NLS-1$
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }

        // Open the file and output streams
        InputStream in = new FileInputStream(tmpFile);

        String mimeType = getServletContext().getMimeType(image);
        if ((null == mimeType) || (mimeType.length() <= 0)) {
            // Hard coded to PNG because BIRT does not give us a mime type at
            // all...
            response.setContentType("image/png"); //$NON-NLS-1$
        } else {
            response.setContentType(mimeType);
        }
        OutputStream out = response.getOutputStream();
        try {
            byte[] buffer = new byte[2048];
            int n, length = 0;
            while ((n = in.read(buffer)) > 0) {
                out.write(buffer, 0, n);
                length += n;
            }
            response.setContentLength(length);
        } finally {
            in.close();
            out.close();
        }
    } finally {
        PentahoSystem.systemExitPoint();
    }

}

From source file:org.dspace.rdf.providing.LocalURIRedirectionServlet.java

/**
 * Processes requests for both HTTP//from   ww  w  . ja va 2  s  . c  o  m
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // we expect a path in the form /resource/<prefix>/<suffix>.
    String pathInfo = request.getPathInfo();

    log.debug("Pathinfo: " + pathInfo);
    if (StringUtils.isEmpty(pathInfo) || StringUtils.countMatches(pathInfo, "/") < 2) {
        log.debug("Path does not contain the expected number of slashes.");
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // remove trailing slash of the path info and split it.
    String[] path = request.getPathInfo().substring(1).split("/");

    String handle = path[0] + "/" + path[1];

    // Prepare content negotiation
    int requestedMimeType = Negotiator.negotiate(request.getHeader(ACCEPT_HEADER_NAME));

    Context context = null;
    DSpaceObject dso = null;
    try {
        context = new Context(Context.Mode.READ_ONLY);
        dso = handleService.resolveToObject(context, handle);
    } catch (SQLException ex) {
        log.error("SQLException: " + ex.getMessage(), ex);
        context.abort();
        // probably a problem with the db connection => send Service Unavailable
        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        return;
    } catch (IllegalStateException ex) {
        log.error("Cannot resolve handle " + handle + ". IllegalStateException:" + ex.getMessage(), ex);
        context.abort();
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    if (dso == null) {
        log.info("Cannot resolve handle '" + handle + "' to dso. => 404");
        context.abort();
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    // close the context and send forward.
    context.abort();
    Negotiator.sendRedirect(response, handle, "", requestedMimeType, true);
}