public static UriComponentsBuilder getURIBuilder(String path, boolean zoneSwitchPossible) { UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentContextPath().path(path); if (zoneSwitchPossible) { String host = builder.build().getHost(); IdentityZone current = IdentityZoneHolder.get(); if (host != null && !IdentityZoneHolder.isUaa()) { if (!host.startsWith(current.getSubdomain() + ".")) { host = current.getSubdomain() + "." + host; builder.host(host); } } } return builder; }
private static UriComponentsBuilder getBaseUri() { return ServletUriComponentsBuilder.fromCurrentContextPath().path(BASE_URI); }
@RequestMapping(method = RequestMethod.POST) HttpEntity<Void> writeUserProfilePhoto(@PathVariable Long user, @RequestParam MultipartFile file) throws Throwable { byte bytesForProfilePhoto[] = FileCopyUtils.copyToByteArray(file.getInputStream()); this.crmService.writeUserProfilePhoto(user, MediaType.parseMediaType(file.getContentType()), bytesForProfilePhoto); HttpHeaders httpHeaders = new HttpHeaders(); URI uriOfPhoto = ServletUriComponentsBuilder.fromCurrentContextPath() .pathSegment(("/users" + "/{user}" + "/photo").substring(1)) .buildAndExpand(Collections.singletonMap("user", user)) .toUri(); httpHeaders.setLocation(uriOfPhoto); return new ResponseEntity<>(httpHeaders, HttpStatus.CREATED); }
@RequestMapping(method = RequestMethod.POST, value = "/{user}/customers") ResponseEntity<Customer> addCustomer(@PathVariable Long user, @RequestBody Customer c) { Customer customer = crmService.addCustomer(user, c.getFirstName(), c.getLastName()); URI uriOfNewResource = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/users/{user}/customers/{customer}") .buildAndExpand(user, customer.getId()) .toUri(); HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.setLocation(uriOfNewResource); return new ResponseEntity<>(customer, httpHeaders, HttpStatus.CREATED); }
/** * Entry point for the user registration process. On successful registration, * publish an event to generate email verification token */ @PostMapping("/register") @ApiOperation(value = "Registers the user and publishes an event to generate the email verification") public ResponseEntity<?> registerUser(@ApiParam(value = "The RegistrationRequest payload") @Valid @RequestBody RegistrationRequest registrationRequest, WebRequest request) { Optional<User> registeredUserOpt = authService.registerUser(registrationRequest); registeredUserOpt.orElseThrow(() -> new UserRegistrationException(registrationRequest.getEmail(), "Missing user object in database")); UriComponentsBuilder urlBuilder = ServletUriComponentsBuilder.fromCurrentContextPath().path("/api/auth" + "/registrationConfirmation"); OnUserRegistrationCompleteEvent onUserRegistrationCompleteEvent = new OnUserRegistrationCompleteEvent(registeredUserOpt.get(), urlBuilder); applicationEventPublisher.publishEvent(onUserRegistrationCompleteEvent); registeredUserOpt.ifPresent(user -> logger.info("Registered User returned [API[: " + user)); return ResponseEntity.ok(new ApiResponse("User registered successfully. Check your email" + " for verification", true)); }
/** * Receives the reset link request and publishes an event to send email id containing * the reset link if the request is valid. In future the deeplink should open within * the app itself. */ @PostMapping("/password/resetlink") @ApiOperation(value = "Receive the reset link request and publish event to send mail containing the password " + "reset link") public ResponseEntity<?> resetLink(@ApiParam(value = "The PasswordResetLinkRequest payload") @Valid @RequestBody PasswordResetLinkRequest passwordResetLinkRequest) { Optional<PasswordResetToken> passwordResetTokenOpt = authService .generatePasswordResetToken(passwordResetLinkRequest); passwordResetTokenOpt.orElseThrow(() -> new PasswordResetLinkException(passwordResetLinkRequest.getEmail(), "Couldn't create a valid token")); PasswordResetToken passwordResetToken = passwordResetTokenOpt.get(); UriComponentsBuilder urlBuilder = ServletUriComponentsBuilder.fromCurrentContextPath().path("/password/reset"); OnGenerateResetLinkEvent generateResetLinkMailEvent = new OnGenerateResetLinkEvent(passwordResetToken, urlBuilder); applicationEventPublisher.publishEvent(generateResetLinkMailEvent); return ResponseEntity.ok(new ApiResponse("Password reset link sent successfully", true)); }
/** * Resend the email registration mail with an updated token expiry. Safe to * assume that the user would always click on the last re-verification email and * any attempts at generating new token from past (possibly archived/deleted) * tokens should fail and report an exception. */ @GetMapping("/resendRegistrationToken") @ApiOperation(value = "Resend the email registration with an updated token expiry. Safe to " + "assume that the user would always click on the last re-verification email and " + "any attempts at generating new token from past (possibly archived/deleted)" + "tokens should fail and report an exception. ") public ResponseEntity<?> resendRegistrationToken(@ApiParam(value = "the initial token that was sent to the user " + " email after registration") @RequestParam("token") String existingToken) { Optional<EmailVerificationToken> newEmailTokenOpt = authService.recreateRegistrationToken(existingToken); newEmailTokenOpt.orElseThrow(() -> new InvalidTokenRequestException("Email Verification Token", existingToken, "User is already registered. No need to re-generate token")); User registeredUser = newEmailTokenOpt.map(EmailVerificationToken::getUser) .orElseThrow(() -> new InvalidTokenRequestException("Email Verification Token", existingToken, "No user associated with this request. Re-verification denied")); UriComponentsBuilder urlBuilder = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/api/auth" + "/registrationConfirmation"); OnRegenerateEmailVerificationEvent regenerateEmailVerificationEvent = new OnRegenerateEmailVerificationEvent( registeredUser, urlBuilder, newEmailTokenOpt.get()); applicationEventPublisher.publishEvent(regenerateEmailVerificationEvent); return ResponseEntity.ok(new ApiResponse("Email verification resent successfully", true)); }
@PostMapping("/uploadFile") public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) { String fileName = fileStorageService.storeFile(file); String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath() .path("/downloadFile/") .path(fileName) .toUriString(); return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize()); }