Example usage for org.springframework.http HttpStatus UNAUTHORIZED

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

Introduction

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

Prototype

HttpStatus UNAUTHORIZED

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

Click Source Link

Document

401 Unauthorized .

Usage

From source file:md.ibanc.rm.controllers.request.RequestCardsController.java

@RequestMapping(value = "/find/cards", method = RequestMethod.POST)
public ResponseEntity<CardsDetails> getCardsByUser(@Valid @RequestBody RequestForm requestForm,
        BindingResult bindingResult, HttpServletRequest request) {

    //        if (bindingResult.hasFieldErrors()) {
    //            return showFieldErrors(bindingResult);
    //        }//from   w  w w . jav  a  2 s.  co  m
    //
    //       // Customers customers = customersService.findCustomersByPersonalId(requestForm.getPersonalId());
    //
    //        CustomerRequestValidator customersValidator = new CustomerRequestValidator(customers, requestForm.getGuid());
    //        customersValidator.validate();
    //
    ////        if (!customersValidator.isIsCustomersValid()) {
    ////            return customersValidator.getResponseEntity();
    ////        }
    Customers customers = customersService.findCustomersBySession(requestForm.getGuid(), null);

    if (customers == null) {
        CardsDetails cardsDetails = new CardsDetails();
        cardsDetails.setReturnCode(RetCodeConst.UNAUTHORIZED);
        cardsDetails.setReturnDescription(RetDescriptionConst.GUID_VALIDATION_ERROR);
        return new ResponseEntity<>(cardsDetails, HttpStatus.UNAUTHORIZED);
    }

    List<Cards> cardsList = cardsService.findCardsByCustomer(customers.getPersonalId());

    CardsDetails cardsDetails = new CardsDetails();

    cardsDetails.setReturnCode(RetCodeConst.RETCOD_OK);
    cardsDetails.setReturnDescription(RetDescriptionConst.RETCOD_OK);
    cardsDetails.setToken(GuidCode.getGuid());

    for (int i = 0; i < cardsList.size(); i++) {
        CardsList list = new CardsList();

        String description = cardsList.get(i).getDescription() + "("
                + cardsList.get(i).getAccounts().getBalance().toString() + " "
                + cardsList.get(i).getAccounts().getValuta().getShortName() + ")";

        list.setStatus(cardsList.get(i).getStatus().getDescription());
        list.setTypes(cardsList.get(i).getTypes().getDescription());
        list.setDescription(description);
        list.setPan(cardsList.get(i).getPan());
        list.setMaskPan(MaskHelper.maskNumber(cardsList.get(i).getPan(), mask));

        cardsDetails.cardsList.add(list);

    }

    //  System.out.println("Json" + new ResponseEntity<>(cardsDetails.getCardsList(), HttpStatus.OK).toString());
    return new ResponseEntity<>(cardsDetails, HttpStatus.OK);

}

From source file:de.petendi.ethereum.secure.proxy.controller.SecureController.java

@ResponseBody
@RequestMapping(method = RequestMethod.POST, value = "/{fingerprint}")
public ResponseEntity<EncryptedMessage> post(@PathVariable("fingerprint") String fingerPrint,
        @RequestBody EncryptedMessage encryptedMessage) {

    IO.UnencryptedResponse unencryptedResponse = new IO.UnencryptedResponse() {
        @Override/*from   ww w.j  a  va 2  s. c  o m*/
        public byte[] getUnencryptedResponse(byte[] bytes, String s, String s1) {
            return SecureController.this.dispatch(bytes).getBytes();
        }
    };
    try {
        EncryptedMessage encrypted = seccoco.io().dispatch(fingerPrint, encryptedMessage, unencryptedResponse);
        return new ResponseEntity<EncryptedMessage>(encrypted, HttpStatus.OK);
    } catch (IO.RequestException e) {
        HttpStatus status;
        if (e instanceof IO.CertificateNotFoundException) {
            status = HttpStatus.FORBIDDEN;
        } else if (e instanceof IO.SignatureCheckFailedException) {
            status = HttpStatus.UNAUTHORIZED;
        } else if (e instanceof IO.InvalidInputException) {
            status = HttpStatus.BAD_REQUEST;
        } else {
            status = HttpStatus.INTERNAL_SERVER_ERROR;
        }
        return new ResponseEntity<EncryptedMessage>(status);
    }
}

From source file:net.orpiske.tcs.service.rest.functional.DomainCreateTest.java

@Test
public void testAuthenticationError() {
    HttpEntity<Domain> requestEntity = new HttpEntity<Domain>(RestDataFixtures.customCsp(),
            getHeaders(USERNAME + ":" + BAD_PASSWORD));

    RestTemplate template = new RestTemplate();

    try {/*from   ww  w.jav  a  2s. c o  m*/
        ResponseEntity<Domain> responseEntity = template
                .postForEntity("http://localhost:8080/tcs/domain/terra.com.br", requestEntity, Domain.class);

        fail("Request Passed incorrectly with status " + responseEntity.getStatusCode());
    } catch (HttpClientErrorException e) {
        assertEquals(HttpStatus.UNAUTHORIZED, e.getStatusCode());
    }
}

From source file:org.sharetask.controller.UserController.java

@RequestMapping(value = "/login/status", method = RequestMethod.GET)
public void loginStatus(final HttpServletRequest request, final HttpServletResponse response) {
    int resultCode = HttpStatus.UNAUTHORIZED.value();
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    log.info("authetication: {}", authentication);
    if (authentication != null && authentication.isAuthenticated()
            && !authentication.getAuthorities().contains(new SimpleGrantedAuthority("ROLE_ANONYMOUS"))) {
        resultCode = HttpStatus.OK.value();
    }//from  ww w .j  a  va  2 s . c om
    response.setStatus(resultCode);
}

From source file:app.api.swagger.SwaggerConfig.java

private List<ResponseMessage> defaultGetResponses() {
    final List<ResponseMessage> results = new ArrayList<ResponseMessage>();
    results.add(response(HttpStatus.FORBIDDEN, null));
    results.add(response(HttpStatus.UNAUTHORIZED, null));
    results.add(response(HttpStatus.NOT_FOUND, null));
    return results;
}

From source file:eu.freme.common.exception.ExceptionHandlerService.java

public ResponseEntity<String> handleError(HttpServletRequest req, Throwable exception) {
    logger.error("Request: " + req.getRequestURL() + " raised ", exception);

    HttpStatus statusCode = null;/*  ww w  .  ja va2 s. com*/
    if (exception instanceof MissingServletRequestParameterException) {
        // create response for spring exceptions
        statusCode = HttpStatus.BAD_REQUEST;
    } else if (exception instanceof FREMEHttpException
            && ((FREMEHttpException) exception).getHttpStatusCode() != null) {
        // get response code from FREMEHttpException
        statusCode = ((FREMEHttpException) exception).getHttpStatusCode();
    } else if (exception instanceof AccessDeniedException) {
        statusCode = HttpStatus.UNAUTHORIZED;
    } else if (exception instanceof HttpMessageNotReadableException) {
        statusCode = HttpStatus.BAD_REQUEST;
    } else {
        // get status code from exception class annotation
        Annotation responseStatusAnnotation = exception.getClass().getAnnotation(ResponseStatus.class);
        if (responseStatusAnnotation instanceof ResponseStatus) {
            statusCode = ((ResponseStatus) responseStatusAnnotation).value();
        } else {
            // set default status code 500
            statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
        }
    }
    JSONObject json = new JSONObject();
    json.put("status", statusCode.value());
    json.put("message", exception.getMessage());
    json.put("error", statusCode.getReasonPhrase());
    json.put("timestamp", new Date().getTime());
    json.put("exception", exception.getClass().getName());
    json.put("path", req.getRequestURI());

    if (exception instanceof AdditionalFieldsException) {
        Map<String, JSONObject> additionalFields = ((AdditionalFieldsException) exception)
                .getAdditionalFields();
        for (String key : additionalFields.keySet()) {
            json.put(key, additionalFields.get(key));
        }
    }

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/json");

    return new ResponseEntity<String>(json.toString(2), responseHeaders, statusCode);
}

From source file:de.thm.arsnova.controller.SecurityExceptionControllerAdvice.java

@ExceptionHandler(AccessDeniedException.class)
public void handleAccessDeniedException(final Exception e, final HttpServletRequest request,
        final HttpServletResponse response) {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || authentication.getPrincipal() == null
            || authentication instanceof AnonymousAuthenticationToken) {
        response.setStatus(HttpStatus.UNAUTHORIZED.value());
        return;//from ww w .  java  2 s  . c  o m
    }
    response.setStatus(HttpStatus.FORBIDDEN.value());
}

From source file:com.todo.backend.web.rest.exception.ExceptionResolver.java

@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AccessDeniedException.class)
public @ResponseBody ErrorResponse accessDenied(HttpServletRequest request, AccessDeniedException exception) {
    if (log.isErrorEnabled()) {
        log.error(exception.getMessage(), exception);
    }/*from   w  ww. jav a  2  s.co m*/
    return new ErrorResponse("access.denied", "Access is denied!");
}

From source file:com.wolkabout.hexiwear.activity.ResetPasswordActivity.java

@Background
void submitResetPassword() {
    showResetPending();/*from   www. j av  a  2s .  c  om*/
    try {
        authenticationService.resetPassword(new ResetPasswordRequest(email.getValue()));
        hideResetPending();
        showInfoDialog(R.string.reset_password_success);
    } catch (HttpStatusCodeException e) {
        Log.e(TAG, "Couldn't reset password", e);
        hideResetPending();
        if (e.getStatusCode() == HttpStatus.UNAUTHORIZED) {
            showWarningDialog(R.string.reset_password_no_such_email_error);
            return;
        }

        showWarningDialog(R.string.reset_password_error);
    } catch (Exception e) {
        Log.e(TAG, "Couldn't reset password", e);
        hideResetPending();
        showWarningDialog(R.string.reset_password_error);
    }
}

From source file:com.wolkabout.hexiwear.activity.PasswordChangeActivity.java

@Click
@Background//from w  w  w.j  a v  a  2  s .  c  o  m
void changePassword() {
    if (!validate()) {
        return;
    }

    showChangePasswordPending();

    try {
        final ChangePasswordRequest requestDto = new ChangePasswordRequest(currentPassword.getValue(),
                newPassword.getValue());
        userService.changePassword(requestDto);
        onPasswordChanged(requestDto);
    } catch (HttpStatusCodeException e) {
        hideChangePasswordPending();
        if (e.getStatusCode() == HttpStatus.BAD_REQUEST || e.getStatusCode() == HttpStatus.UNAUTHORIZED) {
            showWarningDialog(R.string.change_password_old_invalid);
            return;
        }

        showWarningDialog(R.string.change_password_error);
    } catch (Exception e) {
        hideChangePasswordPending();
        showWarningDialog(R.string.change_password_error);
    }
}