Example usage for javax.servlet.http HttpServletResponse setStatus

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

Introduction

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

Prototype

public void setStatus(int sc);

Source Link

Document

Sets the status code for this response.

Usage

From source file:cltestgrid.GetBlob2.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String userAgent = req.getHeader("User-Agent");
    if (userAgent != null && userAgent.contains("Baiduspider")) {
        resp.setStatus(SC_MOVED_PERMANENTLY);
        resp.setHeader("Location", "http://www.baidu.com/search/spider.html");
        resp.setHeader("X-READ-ME",
                "Please honor robots.txt and don't waste our resources. http://cl-test-grid.appspot.com/robots.txt");
        return;/* w w w  . j  a v a  2s .c o  m*/
    }

    final String key = req.getParameter("key");
    GcsFilename filename = new GcsFilename("cl-test-grid-logs", key);
    GcsInputChannel readChannel = null;
    InputStream inputStream = null;
    try {
        final boolean lock = false;
        //readChannel = FileServiceFactory.getFileService().openReadChannel(file, lock);
        readChannel = GcsServiceFactory.createGcsService().openReadChannel(filename, 0);
        inputStream = Channels.newInputStream(readChannel);
        resp.setContentType("text/plain");
        // The log files are gzipped, but the Cloud Storage Client Library ungzips them automatically.
        // After we return this data, GAE gzips our output in case client accepts 'gzip' contect encoding.
        IOUtils.copy(new BufferedInputStream(inputStream, 100 * 1024), resp.getOutputStream());
        resp.getOutputStream().flush();
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(readChannel);
    }
}

From source file:com.palantir.stash.stashbot.servlet.BuildStatusReportingServlet.java

private void printOutput(JSONObject output, HttpServletRequest req, HttpServletResponse res)
        throws IOException {
    res.reset();/*from ww w. ja  v  a 2s .  c o  m*/
    res.setStatus(200);
    res.setContentType("application/json;charset=UTF-8");
    Writer w = res.getWriter();
    try {
        w.append(output.toString(4));
    } catch (JSONException e) {
        w.append(output.toString());
    }
    w.close();
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.admin.SparqlQueryController.java

private void do500InternalServerError(String message, Exception e, HttpServletResponse resp)
        throws IOException {
    resp.setStatus(500);
    PrintWriter w = resp.getWriter();
    w.println(message);/*w  w  w  . j  av  a 2s  .co m*/
    e.printStackTrace(w);
}

From source file:hr.diskobolos.controller.MembershipCategoryController.java

/**
 * REST service responsible for creation of membership category data
 *
 * @param membershipCategory// w ww .  j a va  2 s  . c  o  m
 * @param request
 * @param response
 * @return
 * @throws JSONException
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
@PreAuthorize("hasAnyRole('ROLE_USER','ROLE_ADMIN')")
public String createMembershipCategorysData(@RequestBody MembershipCategory membershipCategory,
        HttpServletRequest request, HttpServletResponse response) throws JSONException {
    try {
        membershipCategoryService.persist(membershipCategory);
        response.setStatus(HttpServletResponse.SC_OK);
        return new JSONObject().put("result", 200).toString();
    } catch (Exception e) {
        logger.error("Error during creation of membership category data: ", e.getMessage());
        return ErrorHandlerUtils.handleAjaxError(request, response);
    }
}

From source file:org.magnum.dataup.VideoUpController.java

@RequestMapping(value = VideoSvcApi.VIDEO_DATA_PATH, method = RequestMethod.GET)
public void getData(@PathVariable(VideoSvcApi.ID_PARAMETER) long id, HttpServletResponse resp) {
    if (videos.containsKey(id)) {
        try {//from   w  w  w  .  j  a  va  2  s .  c  o  m
            serveVideo(videos.get(id), resp);
            resp.setStatus(HttpServletResponse.SC_OK);
            resp.setHeader("Content-Type", "video/mpeg");
        } catch (IOException e) {
            resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        }
    } else {
        resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.magnum.mobilecloud.video.MainController_HW_2.java

@RequestMapping(value = VideoSvcApi.VIDEO_SVC_PATH + "{/id}", method = RequestMethod.GET)
public @ResponseBody Video getVideo(@PathVariable("id") long id, HttpServletResponse response) {
    Video v = videos.findOne(id);//www.j a va  2 s. c o  m
    if (v == null) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    }
    return v;
}

From source file:de.yaio.services.webshot.server.controller.WebshotController.java

@ExceptionHandler(PermissionException.class)
public void handleCustomException(final HttpServletRequest request, final PermissionException e,
        final HttpServletResponse response) throws IOException {
    LOGGER.info("Exception while running request:" + createRequestLogMessage(request), e);
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    response.getWriter().append("permission denied (firewall...) while webshoting resource");
}

From source file:cltestgrid.GetBlob.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
    String userAgent = req.getHeader("User-Agent");
    if (userAgent != null && userAgent.contains("Baiduspider")) {
        resp.setStatus(SC_MOVED_PERMANENTLY);
        resp.setHeader("Location", "http://www.baidu.com/search/spider.html");
        resp.setHeader("X-READ-ME",
                "Please honor robots.txt and don't waste our resources. http://cl-test-grid.appspot.com/robots.txt");
        return;/*from w w w.  j a  va  2s  .co m*/
    }

    final String key = req.getParameter("key");
    String filename = "/gs/cl-test-grid-logs/" + key;
    AppEngineFile file = new AppEngineFile(filename);
    FileReadChannel readChannel = null;
    InputStream inputStream = null;
    try {
        final boolean lock = false;
        readChannel = FileServiceFactory.getFileService().openReadChannel(file, lock);
        inputStream = Channels.newInputStream(readChannel);
        resp.setContentType("text/plain");
        // The log files are gzipped, but we can't serve gzipped content,
        // because GAE gzips servlet output itlself and so logs would end-up gzipped twice.
        // Therefore we need to ungzip the log.
        InputStream ungzipper = new GZIPInputStream(new BufferedInputStream(inputStream, 100 * 1024));
        IOUtils.copy(ungzipper, resp.getOutputStream());
        resp.getOutputStream().flush();
    } finally {
        IOUtils.closeQuietly(inputStream);
        IOUtils.closeQuietly(readChannel);
    }
}

From source file:com.thoughtworks.go.server.plugin.controller.PluginController.java

@RequestMapping(value = "/plugin/interact/{pluginId}/{requestName}", method = { RequestMethod.GET,
        RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE })
public void handlePluginInteractRequest(@PathVariable String pluginId, @PathVariable String requestName,
        HttpServletRequest request, HttpServletResponse response) throws IOException {

    if (!isAuthPlugin(pluginId)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter().println("Plugin interact endpoint is enabled only for Authentication Plugins");
        return;/* w  w  w.  ja  va2s . c o  m*/
    }

    if (isRestrictedRequestName(requestName)) {
        response.setStatus(SC_FORBIDDEN);
        response.getWriter()
                .println(String.format("Plugin interact for '%s' requestName is disallowed.", requestName));
        return;
    }

    DefaultGoPluginApiRequest apiRequest = new DefaultGoPluginApiRequest(null, null, requestName);
    apiRequest.setRequestParams(getParameterMap(request));
    addRequestHeaders(request, apiRequest);

    try {
        GoPluginApiResponse pluginApiResponse = pluginManager.submitTo(pluginId, apiRequest);

        if (DefaultGoApiResponse.SUCCESS_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            renderPluginResponse(pluginApiResponse, response);
            return;
        }
        if (DefaultGoApiResponse.REDIRECT_RESPONSE_CODE == pluginApiResponse.responseCode()) {
            String location = "";
            if (hasValueFor(pluginApiResponse, "Location")) {
                location = pluginApiResponse.responseHeaders().get("Location");
            }
            response.sendRedirect(location);
            return;
        }
    } catch (Exception e) {
        // handle
    }
    throw new RuntimeException("render error page");
}