Example usage for org.springframework.http HttpStatus FORBIDDEN

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

Introduction

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

Prototype

HttpStatus FORBIDDEN

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

Click Source Link

Document

403 Forbidden .

Usage

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

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public CustomResponseMessage getAllUsers(@RequestHeader String apiKey) {
    try {/*from w w  w  . j  av a2s.c om*/
        if (!authApiKey(apiKey)) {
            return new CustomResponseMessage(HttpStatus.FORBIDDEN, "Please use your api key to authentication");
        }

        if (checkRateLimit(RATE_LIMIT, apiKey)) {
            return new CustomResponseMessage(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED,
                    "Please wait a while, you have reached your rate limit");
        }

        LogRequest temp = DependencyFactory.createLog(apiKey, "Get");

        Log log = new Log();
        log.setApiKey(temp.getApiKey());
        log.setStatus(temp.getStatus());
        log.setTimeRequest(temp.getTime_request());
        logService.addLog(log);

        List<User> allUsers = (List<User>) userService.getAllUsers();
        for (User user : allUsers) {
            Link selfLink = linkTo(UserController.class).withSelfRel();
            user.add(selfLink);
        }
        CustomResponseMessage result = new CustomResponseMessage();
        result.add(linkTo(UserController.class).withSelfRel());
        result.setHttpStatus(HttpStatus.ACCEPTED);
        result.setMessage("Success");
        result.setResult(allUsers);
        return result;
    } catch (Exception ex) {
        return new CustomResponseMessage(HttpStatus.FORBIDDEN, "Please use your api key to authentication");
    }
}

From source file:de.hska.ld.core.controller.RoleControllerIntegrationTest.java

@Test
public void testSaveRoleUsesHttpForbiddenOnAuthorizationFailure() throws Exception {

    HttpResponse response = UserSession.user().post(RESOURCE_ROLE, newRole());
    Assert.assertEquals(HttpStatus.FORBIDDEN, ResponseHelper.getStatusCode(response));
}

From source file:de.steilerdev.myVerein.server.controller.user.MessageController.java

@RequestMapping(produces = "application/json", method = RequestMethod.GET, params = "id")
public ResponseEntity<Message> getMessage(@RequestParam(value = "id") String messageId,
        @CurrentUser User currentUser) {
    logger.trace("[{}] Getting message with id {}", currentUser, messageId);
    Message message;/*from w  w  w.  ja v a  2  s.c  om*/
    if (messageId.isEmpty()) {
        logger.warn("[{}] Required parameter id missing", currentUser);
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } else if ((message = messageRepository.findOne(messageId)) == null) {
        logger.warn("[{}] Unable to find message with id {}", currentUser, messageId);
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } else if (!message.getReceiver().containsKey(currentUser.getId())) {
        logger.warn("[{}] User is not allowed to read message {}, because he is not a receiver", currentUser,
                message);
        return new ResponseEntity<>(HttpStatus.FORBIDDEN);
    } else {
        logger.info("[{}] Delivering message {}", currentUser, message);
        return new ResponseEntity<>(message.getSendingObjectInternalSync(), HttpStatus.OK);
    }
}

From source file:fr.itldev.koya.services.impl.util.AlfrescoRestErrorHandler.java

@Override
public void handleError(ClientHttpResponse clienthttpresponse) throws IOException {

    if (!statusOK.contains(clienthttpresponse.getStatusCode())) {
        AlfrescoServiceException ex;/* w  w  w  .ja  v  a 2 s  .c o  m*/
        if (clienthttpresponse.getStatusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
            java.util.Scanner s = new java.util.Scanner(clienthttpresponse.getBody()).useDelimiter("\\A");
            String message = s.hasNext() ? s.next() : "";

            /*
             Try to get any Koya Error code if exists
             */
            Integer koyaErrorCode = null;

            Matcher matcher = ERRORCODEPATTERN.matcher(message);
            if (matcher.find()) {
                koyaErrorCode = Integer.valueOf(matcher.group(1));
            }

            ex = new AlfrescoServiceException(
                    "Erreur " + clienthttpresponse.getStatusCode() + " : " + clienthttpresponse.getStatusText(),
                    koyaErrorCode);
            ex.setHttpErrorCode(clienthttpresponse.getStatusCode().value());
        } else if (clienthttpresponse.getStatusCode().equals(HttpStatus.FORBIDDEN)) {
            ex = new AlfrescoServiceException("Acces Denied");
            ex.setHttpErrorCode(clienthttpresponse.getStatusCode().value());

        } else {
            ex = new AlfrescoServiceException();
            ex.setHttpErrorCode(clienthttpresponse.getStatusCode().value());
            throw ex;
        }
        throw ex;

    }
}

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

@RequestMapping(method = RequestMethod.GET, produces = "application/json")
public CustomResponseMessage /*Collection<Barang>*/ getAllBarang(@RequestHeader String apiKey) {
    try {/*w  w  w  . java 2  s.c  o  m*/

        if (!authApiKey(apiKey)) {
            return new CustomResponseMessage(HttpStatus.FORBIDDEN, "Please use your api key to authentication");
        }
        LogRequest temp = DependencyFactory.createLog(apiKey, "Get");

        if (checkRateLimit(RATE_LIMIT, apiKey)) {
            return new CustomResponseMessage(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED,
                    "Please wait a while, you have reached your rate limit");
        }
        Log log = new Log();
        log.setApiKey(temp.getApiKey());
        log.setStatus(temp.getStatus());
        log.setTimeRequest(temp.getTime_request());
        logService.addLog(log);

        List<Barang> allBarang = (List<Barang>) barangService.getAllBarang();
        for (Barang barang : allBarang) {
            Link selfLink = linkTo(BarangController.class).withSelfRel();
            barang.add(selfLink);
        }
        CustomResponseMessage result = new CustomResponseMessage();
        result.add(linkTo(BarangController.class).withSelfRel());
        result.setHttpStatus(HttpStatus.FOUND);
        result.setMessage("Success");
        result.setResult(allBarang);
        return result;
    } catch (NullPointerException ex) {
        return new CustomResponseMessage(HttpStatus.NOT_FOUND, "Data not found");
    } catch (Exception ex) {
        return new CustomResponseMessage(HttpStatus.FORBIDDEN, ex.getMessage());
    }
}

From source file:org.mitre.oauth2.web.AccessTokenAPI.java

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
public String getById(@PathVariable("id") Long id, ModelMap m, Principal p) {

    OAuth2AccessTokenEntity token = tokenService.getAccessTokenById(id);

    if (token == null) {
        logger.error("getToken failed; token not found: " + id);
        m.put("code", HttpStatus.NOT_FOUND);
        m.put("errorMessage", "The requested token with id " + id + " could not be found.");
        return "jsonErrorView";
    } else if (!token.getAuthenticationHolder().getAuthentication().getName().equals(p.getName())) {
        logger.error("getToken failed; token does not belong to principal " + p.getName());
        m.put("code", HttpStatus.FORBIDDEN);
        m.put("errorMessage", "You do not have permission to view this token");
        return "jsonErrorView";
    } else {/*from w w w. ja v a2  s. c  o  m*/
        m.put("entity", token);
        return "jsonEntityView";
    }
}

From source file:com.hp.autonomy.frontend.find.core.beanconfiguration.AppConfiguration.java

@SuppressWarnings("ReturnOfInnerClass")
@Bean//from   www.  j  a v a2s .c o  m
public EmbeddedServletContainerCustomizer containerCustomizer() {

    return new EmbeddedServletContainerCustomizer() {
        @Override
        public void customize(final ConfigurableEmbeddedServletContainer container) {

            final ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED,
                    DispatcherServletConfiguration.AUTHENTICATION_ERROR_PATH);
            final ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN,
                    DispatcherServletConfiguration.AUTHENTICATION_ERROR_PATH);
            final ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND,
                    DispatcherServletConfiguration.NOT_FOUND_ERROR_PATH);
            final ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,
                    DispatcherServletConfiguration.SERVER_ERROR_PATH);

            container.addErrorPages(error401Page, error403Page, error404Page, error500Page);
        }
    };
}

From source file:fi.helsinki.opintoni.config.WebConfigurer.java

@Bean
public EmbeddedServletContainerCustomizer containerCustomizer() {

    return (container -> {
        ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/errors/403.html");
        ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/errors/404.html");
        ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/errors/500.html");

        container.addErrorPages(error403Page, error404Page, error500Page);
    });//  w  w  w  . ja v a  2 s. co m
}

From source file:org.mitre.oauth2.web.RefreshTokenAPI.java

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
public String getById(@PathVariable("id") Long id, ModelMap m, Principal p) {

    OAuth2RefreshTokenEntity token = tokenService.getRefreshTokenById(id);

    if (token == null) {
        logger.error("getToken failed; token not found: " + id);
        m.put("code", HttpStatus.NOT_FOUND);
        m.put("errorMessage", "The requested token with id " + id + " could not be found.");
        return "jsonErrorView";
    } else if (!token.getAuthenticationHolder().getAuthentication().getName().equals(p.getName())) {
        logger.error("getToken failed; token does not belong to principal " + p.getName());
        m.put("code", HttpStatus.FORBIDDEN);
        m.put("errorMessage", "You do not have permission to view this token");
        return "jsonErrorView";
    } else {/*from  w ww .j  av a2s  .c o  m*/
        m.put("entity", token);
        return "jsonEntityView";
    }
}

From source file:io.kahu.hawaii.util.exception.HawaiiExceptionTest.java

@Test
public void testGetStatus() throws Exception {

    ServerException e = new ServerException(TestServerError.S1);
    Assert.assertTrue(HttpStatus.INTERNAL_SERVER_ERROR.equals(e.getStatus()));

    AuthenticationException auth = new AuthenticationException();
    Assert.assertTrue(HttpStatus.FORBIDDEN.equals(auth.getStatus()));

    AuthorisationException autho = new AuthorisationException();
    Assert.assertTrue(HttpStatus.UNAUTHORIZED.equals(autho.getStatus()));

    ValidationException v = new ValidationException();
    Assert.assertTrue(HttpStatus.BAD_REQUEST.equals(v.getStatus()));
}