Example usage for javax.servlet.http HttpServletResponse SC_BAD_REQUEST

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

Introduction

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

Prototype

int SC_BAD_REQUEST

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

Click Source Link

Document

Status code (400) indicating the request sent by the client was syntactically incorrect.

Usage

From source file:org.imsglobal.lti2.LTI2Servlet.java

@SuppressWarnings("unused")
protected void doRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println("getServiceURL=" + getServiceURL(request));

    String ipAddress = request.getRemoteAddr();
    System.out.println("LTI Service request from IP=" + ipAddress);

    String rpi = request.getPathInfo();
    String uri = request.getRequestURI();
    String[] parts = uri.split("/");
    if (parts.length < 4) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        doErrorJSON(request, response, null, "Incorrect url format", null);
        return;//ww w. j  av a 2s.  c o m
    }
    String controller = parts[3];
    if ("register".equals(controller)) {
        doRegister(request, response);
        return;
    } else if ("launch".equals(controller)) {
        doLaunch(request, response);
        return;
    } else if (SVC_tc_profile.equals(controller) && parts.length == 5) {
        String profile_id = parts[4];
        getToolConsumerProfile(request, response, profile_id);
        return;
    } else if (SVC_tc_registration.equals(controller) && parts.length == 5) {
        String profile_id = parts[4];
        registerToolProviderProfile(request, response, profile_id);
        return;
    } else if (SVC_Result.equals(controller) && parts.length == 5) {
        String sourcedid = parts[4];
        handleResultRequest(request, response, sourcedid);
        return;
    } else if (SVC_Settings.equals(controller) && parts.length >= 6) {
        handleSettingsRequest(request, response, parts);
        return;
    }

    IMSJSONRequest jsonRequest = new IMSJSONRequest(request);
    if (jsonRequest.valid) {
        System.out.println(jsonRequest.getPostBody());
    }

    response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
    M_log.log(Level.WARNING, "Unknown request=" + uri);
    doErrorJSON(request, response, null, "Unknown request=" + uri, null);
}

From source file:ch.admin.suis.msghandler.servlet.TriggerServlet.java

/**
 * Either sends the user an error informing him of a malformed request, or grants its request.
 *
 * @param request  The request/*from  w  ww. j a  v a 2s.  com*/
 * @param response The response
 * @throws IOException        IO problems...
 * @throws SchedulerException The scheduler did not like what you said.
 */
private void dispatchSendRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, SchedulerException {

    final String outboxName = request.getParameter("name");
    if (StringUtils.isNotBlank(outboxName)) {
        if (!handleSend(response, outboxName)) {
            String msg = "No outbox directory found with name: " + outboxName;
            LOG.warn(msg);
            response.getWriter().println(msg);
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    } else {
        String msg = "The action=send requires also 'name' parameter. Which is the name of the outbox which should be sent.";
        LOG.warn(msg);
        response.getWriter().println(msg);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }
}

From source file:io.github.howiefh.jeews.modules.oauth2.controller.AccessTokenController.java

private HttpEntity<String> buildBadAuthCodeResponse() throws OAuthSystemException {
    OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
            .setError(OAuthError.TokenResponse.INVALID_GRANT).setErrorDescription(Constants.INVALID_OAUTH_CODE)
            .buildJSONMessage();//from  w  w w .j  av a  2 s .  co m
    return new ResponseEntity<String>(response.getBody(), HttpStatus.valueOf(response.getResponseStatus()));
}

From source file:com.aipo.container.protocol.AipoDataServiceServlet.java

@Override
protected void doPost(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {
    try {/*www  . j  a v a 2 s  . c o m*/
        checkContentTypes(ALLOWED_CONTENT_TYPES, servletRequest.getContentType());
        executeRequest(servletRequest, servletResponse);
    } catch (ContentTypes.InvalidContentTypeException icte) {
        sendError(servletResponse, new ResponseItem(HttpServletResponse.SC_BAD_REQUEST, icte.getMessage(),
                AipoErrorCode.BAD_REQUEST.responseJSON()));
    }
}

From source file:org.energyos.espi.datacustodian.web.api.BatchRESTController.java

/**
 * Supports Green Button Download My Data - A DMD file will be produced that
 * contains all Usage Points for the requested Retail Customer.
 * /*w  ww.java 2 s  .  co m*/
 * Requires Authorization: Bearer [{data_custodian_access_token} |
 * {upload_access_token}]
 * 
 * @param response
 *            HTTP Servlet Response
 * @param retailCustomerId
 *            The locally unique identifier of a Retail Customer - NOTE PII
 * @param HTTP
 *            Query Parameters
 * @throws IOException
 * @throws FeedException
 * 
 * @usage GET
 *        /espi/1_1/resource/Batch/RetailCustomer/{retailCustomerId}/UsagePoint
 */
@RequestMapping(value = Routes.BATCH_DOWNLOAD_MY_DATA_COLLECTION, method = RequestMethod.GET, produces = "application/atom+xml")
@ResponseBody
public void download_collection(HttpServletResponse response, @PathVariable Long retailCustomerId,
        @RequestParam Map<String, String> params) throws IOException, FeedException {

    response.setContentType(MediaType.APPLICATION_ATOM_XML_VALUE);
    response.addHeader("Content-Disposition", "attachment; filename=GreenButtonDownload.xml");
    try {
        // TODO -- need authorization hook
        exportService.exportUsagePointsFull(0L, retailCustomerId, response.getOutputStream(),
                new ExportFilter(params));

    } catch (Exception e) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    }

}

From source file:net.handle.servlet.OpenHandleServlet.java

/**
 * <p>//from   www  .  j ava 2 s  . com
 * </p>
 *
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    RequestAdapter adapter = new RequestAdapter(request, settings);
    if (isBlank(adapter.getHandle())) {
        doErrorResponse(adapter, request, response, "No handle identifier specified",
                HttpServletResponse.SC_BAD_REQUEST, HttpServletResponse.SC_BAD_REQUEST);
    } else {
        doHandleResponse(adapter, request, response);
    }
}

From source file:org.energyos.espi.datacustodian.web.api.MeterReadingRESTController.java

@RequestMapping(value = Routes.ROOT_METER_READING_MEMBER, method = RequestMethod.PUT, consumes = "application/atom+xml", produces = "application/atom+xml")
@ResponseBody/*w  w  w.j ava 2s .  c om*/
public void update(HttpServletResponse response, @PathVariable Long meterReadingId,
        @RequestParam Map<String, String> params, InputStream stream) {

    if (null != resourceService.findById(meterReadingId, MeterReading.class)) {
        try {
            // note that the import service is doing the merge
            // if this should change, we have to do it here.
            meterReadingService.importResource(stream);

        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        }
    }
}

From source file:com.data2semantics.yasgui.server.servlets.SparqlServlet.java

@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String query = request.getParameter("query");
    String endpoint = request.getParameter("endpoint");

    String accept = request.getHeader("Accept");
    if (accept == null)
        accept = "application/sparql-results+xml";
    String requestMethod = request.getParameter("requestMethod");
    if (query != null && query.length() > 0 && endpoint != null && endpoint.length() > 0) {
        HttpClient client = new DefaultHttpClient();
        PrintWriter out = response.getWriter();
        HttpResponse endpointResponse;//from w  ww .j  a v a  2  s  .  c  o m
        if (requestMethod.equals("GET")) {
            endpointResponse = executeGet(endpoint, accept, client,
                    getNameValuePairs(request.getParameterMap()));
        } else {
            endpointResponse = executePost(endpoint, accept, client,
                    getNameValuePairs(request.getParameterMap()));
        }

        int endpointStatusCode = endpointResponse.getStatusLine().getStatusCode();
        if (endpointStatusCode >= 400) {
            //only return this statuscode when it is an error. Redirection codes (e.g. 302) are allowed I guess
            String reason = endpointResponse.getStatusLine().getReasonPhrase();

            //reasonphrase is often not verbose enough, so append content of page (which often contains actual verbose error msg)
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(endpointResponse.getEntity().getContent()));

            String line;
            while ((line = rd.readLine()) != null) {
                reason += "<br/>" + line;
            }
            response.sendError(endpointStatusCode, reason);

        } else if (endpointStatusCode >= 300) {
            //redirect
            Header[] headers = endpointResponse.getHeaders("Location");
            if (headers.length > 0) {
                String location = headers[0].getValue();
                try {
                    response.sendRedirect(Helper.getAbsoluteRedirectPath(endpoint, location));
                } catch (URISyntaxException e) {
                    response.sendError(500,
                            "Endpoint issued a redirect, but we could not parse the given redirect location: "
                                    + location);
                }
            } else {
                response.sendError(500, "Endpoint issued a redirect, but no redirect url was given");
            }
        } else {
            //Header should be what we asked. It might be something else
            //Copy response header to the new response
            Header[] headers = endpointResponse.getHeaders("Content-Type");
            String contentType = accept;
            if (headers.length > 0) {
                //Just get first defined content type
                contentType = headers[0].getValue();
            }
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(endpointResponse.getEntity().getContent()));
            String line;
            response.setContentType(contentType);
            while ((line = rd.readLine()) != null) {
                out.println(line);
            }
            out.close();
        }
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        String errorMsg = "";
        if (endpoint == null) {
            errorMsg += "No endpoint provided as parameter\n";
        } else {
            errorMsg += "Endpoint: '" + endpoint + "'\n";
        }
        if (query == null) {
            errorMsg += "No sparql provided as parameter\n";
        } else {
            errorMsg += "Sparql query: '" + query + "'\n";
        }
        onError("Invalid query parameters: \n" + errorMsg);
    }
}