Example usage for org.springframework.http HttpStatus METHOD_NOT_ALLOWED

List of usage examples for org.springframework.http HttpStatus METHOD_NOT_ALLOWED

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus METHOD_NOT_ALLOWED.

Prototype

HttpStatus METHOD_NOT_ALLOWED

To view the source code for org.springframework.http HttpStatus METHOD_NOT_ALLOWED.

Click Source Link

Document

405 Method Not Allowed .

Usage

From source file:com.ge.predix.web.cors.CORSFilter.java

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

    if (!isCrossOriginRequest(request)) {
        filterChain.doFilter(request, response);
        return;/*  ww  w  .  j ava  2 s  .c om*/
    }

    if (isXhrRequest(request)) {
        String method = request.getMethod();
        if (!isCorsXhrAllowedMethod(method)) {
            response.setStatus(HttpStatus.METHOD_NOT_ALLOWED.value());
            return;
        }
        String origin = request.getHeader(HttpHeaders.ORIGIN);
        // Validate the origin so we don't reflect back any potentially dangerous content.
        URI originURI;
        try {
            originURI = new URI(origin);
        } catch (URISyntaxException e) {
            response.setStatus(HttpStatus.FORBIDDEN.value());
            return;
        }

        String requestUri = request.getRequestURI();
        if (!isCorsXhrAllowedRequestUri(requestUri) || !isCorsXhrAllowedOrigin(origin)) {
            response.setStatus(HttpStatus.FORBIDDEN.value());
            return;
        }
        response.addHeader("Access-Control-Allow-Origin", originURI.toString());
        if ("OPTIONS".equals(request.getMethod())) {
            buildCorsXhrPreFlightResponse(request, response);
        } else {
            filterChain.doFilter(request, response);
        }
        return;
    }

    response.addHeader("Access-Control-Allow-Origin", "*");
    if (request.getHeader("Access-Control-Request-Method") != null && "OPTIONS".equals(request.getMethod())) {
        // CORS "pre-flight" request
        response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
        response.addHeader("Access-Control-Allow-Headers", "Authorization");
        response.addHeader("Access-Control-Max-Age", "1728000");
    } else {
        filterChain.doFilter(request, response);
    }
}

From source file:org.springsource.restbucks.payment.web.PaymentController.java

/**
 * Takes the {@link Receipt} for the given {@link Order} and thus completes the process.
 *
 * @param order/*  w w  w .j  a  va2 s.  c om*/
 * @return
 */
@RequestMapping(path = PaymentLinks.RECEIPT, method = DELETE)
HttpEntity<?> takeReceipt(@PathVariable("id") Order order) {

    if (order == null || !order.isPaid()) {
        return ResponseEntity.notFound().build();
    }

    return paymentService.takeReceiptFor(order).//
            map(receipt -> createReceiptResponse(receipt)).//
            orElseGet(() -> new ResponseEntity<>(HttpStatus.METHOD_NOT_ALLOWED));
}

From source file:org.appverse.web.framework.backend.test.util.frontfacade.mvc.tests.predefined.BasicAuthEndPointsDisabledPredefinedTests.java

@Test
public void basicAuthenticationServiceTest() throws Exception {
    int port = context.getEmbeddedServletContainer().getPort();

    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization",
            "Basic " + new String(Base64.encode((getUsername() + ":" + getPassword()).getBytes("UTF-8"))));
    HttpEntity<String> entity = new HttpEntity<String>("headers", headers);

    ResponseEntity<AuthorizationData> responseEntity = restTemplate.exchange(
            "http://localhost:" + port + baseApiPath + basicAuthenticationEndpointPath, HttpMethod.POST, entity,
            AuthorizationData.class);
    // When an enpoint is disabled, "405 - METHOD NOT ALLOWED" is returned
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED, responseEntity.getStatusCode());
}

From source file:com.novation.eligibility.rest.spring.web.servlet.handler.DefaultRestErrorResolver.java

protected final Map<String, String> createDefaultExceptionMappingDefinitions() {

    Map<String, String> m = new LinkedHashMap<String, String>();

    // 400//from w ww .j av  a2 s  . c  o  m
    applyDef(m, HttpMessageNotReadableException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, MissingServletRequestParameterException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, TypeMismatchException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, "javax.validation.ValidationException", HttpStatus.BAD_REQUEST);

    // 404
    applyDef(m, NoSuchRequestHandlingMethodException.class, HttpStatus.NOT_FOUND);
    applyDef(m, "org.hibernate.ObjectNotFoundException", HttpStatus.NOT_FOUND);

    // 405
    applyDef(m, HttpRequestMethodNotSupportedException.class, HttpStatus.METHOD_NOT_ALLOWED);

    // 406
    applyDef(m, HttpMediaTypeNotAcceptableException.class, HttpStatus.NOT_ACCEPTABLE);

    // 409
    //can't use the class directly here as it may not be an available dependency:
    applyDef(m, "org.springframework.dao.DataIntegrityViolationException", HttpStatus.CONFLICT);

    // 415
    applyDef(m, HttpMediaTypeNotSupportedException.class, HttpStatus.UNSUPPORTED_MEDIA_TYPE);

    return m;
}

From source file:com.oolong.platform.web.error.DefaultRestErrorResolver.java

protected final Map<String, String> createDefaultExceptionMappingDefinitions() {

    Map<String, String> m = new LinkedHashMap<String, String>();

    // 400/*from   w w w. j ava 2  s. c om*/
    applyDef(m, HttpMessageNotReadableException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, MissingServletRequestParameterException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, TypeMismatchException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, "javax.validation.ValidationException", HttpStatus.BAD_REQUEST);

    // 404
    applyDef(m, NoSuchRequestHandlingMethodException.class, HttpStatus.NOT_FOUND);
    applyDef(m, "org.hibernate.ObjectNotFoundException", HttpStatus.NOT_FOUND);

    // 405
    applyDef(m, HttpRequestMethodNotSupportedException.class, HttpStatus.METHOD_NOT_ALLOWED);

    // 406
    applyDef(m, HttpMediaTypeNotAcceptableException.class, HttpStatus.NOT_ACCEPTABLE);

    // 409
    // can't use the class directly here as it may not be an available
    // dependency:
    applyDef(m, "org.springframework.dao.DataIntegrityViolationException", HttpStatus.CONFLICT);

    // 415
    applyDef(m, HttpMediaTypeNotSupportedException.class, HttpStatus.UNSUPPORTED_MEDIA_TYPE);

    return m;
}

From source file:org.appverse.web.framework.backend.test.util.frontfacade.mvc.tests.predefined.BasicAuthEndPointsDisabledPredefinedTests.java

@Test
public void simpleAuthenticationServiceTest() throws Exception {
    int port = context.getEmbeddedServletContainer().getPort();

    CredentialsVO credentialsVO = new CredentialsVO();
    credentialsVO.setUsername(getUsername());
    credentialsVO.setPassword(getPassword());
    HttpEntity<CredentialsVO> entity = new HttpEntity<CredentialsVO>(credentialsVO);

    ResponseEntity<AuthorizationData> responseEntity = restTemplate.exchange(
            "http://localhost:" + port + baseApiPath + simpleAuthenticationEndpointPath, HttpMethod.POST,
            entity, AuthorizationData.class);
    // When an enpoint is disabled, "405 - METHOD NOT ALLOWED" is returned
    assertEquals(HttpStatus.METHOD_NOT_ALLOWED, responseEntity.getStatusCode());
}

From source file:plbtw.klmpk.barang.hilang.controller.DeveloperController.java

@RequestMapping(method = RequestMethod.POST, produces = "application/json")
public CustomResponseMessage addDeveloper(@RequestBody DeveloperRequest developerRequest) {
    try {/*from  w  w  w.  j a  v a 2 s .c o m*/

        if (developerService.checkDeveloperExist(developerRequest.getEmail()) != null) {
            return new CustomResponseMessage(HttpStatus.METHOD_NOT_ALLOWED, "Email Already Used");
        }

        Developer developer = new Developer();
        developer.setRole(roleService.getRole(developerRequest.getIdrole()));
        SecureRandom random = new SecureRandom();
        byte bytes[] = new byte[20];
        random.nextBytes(bytes);
        String token = bytes.toString();
        developer.setToken(token);
        developer.setSecretKey(token);
        developer.setEmail(developerRequest.getEmail());
        developer.setPassword(developerRequest.getPassword());
        developerService.addDeveloper(developer);
        List<Developer> result = new ArrayList<>();
        result.add(developer);
        return new CustomResponseMessage(HttpStatus.CREATED, "Developer Has Been Created", result);
    } catch (Exception ex) {
        return new CustomResponseMessage(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage());
    }
}

From source file:com.yang.oa.commons.exception.DefaultRestErrorResolver.java

protected final Map<String, String> createDefaultExceptionMappingDefinitions() {

    Map<String, String> m = new LinkedHashMap<String, String>();

    // 400/*w ww.j  a  va  2 s  .co m*/
    applyDef(m, HttpMessageNotReadableException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, MissingServletRequestParameterException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, TypeMismatchException.class, HttpStatus.BAD_REQUEST);
    applyDef(m, "javax.validation.ValidationException", HttpStatus.BAD_REQUEST);
    applyDef(m, JlException.class, HttpStatus.BAD_REQUEST);
    //401
    applyDef(m, org.apache.shiro.authz.UnauthorizedException.class, HttpStatus.UNAUTHORIZED);
    applyDef(m, org.apache.shiro.authz.UnauthenticatedException.class, HttpStatus.UNAUTHORIZED);
    // 404
    applyDef(m, NoSuchRequestHandlingMethodException.class, HttpStatus.NOT_FOUND);
    applyDef(m, "org.hibernate.ObjectNotFoundException", HttpStatus.NOT_FOUND);

    // 405
    applyDef(m, HttpRequestMethodNotSupportedException.class, HttpStatus.METHOD_NOT_ALLOWED);

    // 406
    applyDef(m, HttpMediaTypeNotAcceptableException.class, HttpStatus.NOT_ACCEPTABLE);

    // 409
    //can't use the class directly here as it may not be an available dependency:
    applyDef(m, "org.springframework.dao.DataIntegrityViolationException", HttpStatus.CONFLICT);

    // 415
    applyDef(m, HttpMediaTypeNotSupportedException.class, HttpStatus.UNSUPPORTED_MEDIA_TYPE);

    return m;
}

From source file:org.trustedanalytics.user.invite.RestErrorHandler.java

@ResponseBody
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public String userExists(HttpRequestMethodNotSupportedException e) throws IOException {
    return e.getMessage();
}