0

I have the following REST endpoint:

private static final String TOKEN_HEADER = "Authorization";

@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "OK"),
        @ApiResponse(responseCode = "404", description = "NOT_FOUND")
})
@GetMapping(value = "/book/{ISBN}",
        produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EntityModel<Book>> getBook(
        @PathVariable(name = "ISBN") String isbn,
        @RequestParam(defaultValue = "true") Boolean verbose) {
    createLoggerMessage(Thread.currentThread().getStackTrace()[1].getMethodName());

    final String token = httpServletRequest.getHeader(TOKEN_HEADER).substring(7);

  // call validateToken

    Book books = bookService.getBook(isbn, verbose);

    return ResponseEntity.status(HttpStatus.OK)
            .body(bookAssembler.toModel(books));
}

And a SOAP endpoint:

private static final String NAMESPACE_URI = "http://com.pos.JWT/Token";

private final TokenService validateTokenService;


@PayloadRoot(namespace = NAMESPACE_URI, localPart = "Request-ValidateToken")
@ResponsePayload
public ResponseValidateToken validateToken(@RequestPayload RequestValidateToken input) {
    return validateTokenService.validateToken(input.getToken());
}
public ResponseValidateToken validateToken(String token) {

    if (blackList.contains(token)) {
        throw new RuntimeException("Token invalid");
    }

    if (!jwtTokenUtil.isValidToken(token)) {
        blackList.add(token);
        throw new RuntimeException("Token invalid");
    }

    final String username = jwtTokenUtil.getUsernameFromToken(token);
    final String role = jwtTokenUtil.getRoleFromToken(token);

    checkIfUserFromTokenExists(username);

    return setResponse(username, role);
}

I get the token through header and I want to call from REST the SOAP endpoint for validating the token, how I could do that?

I tried to set HttpHeader with content-type MediaType.TEXT_XML and BearerAuth with the token from header, but I dont know how I could send the request to server

P.S: I forgot to mention, they are in separate modules

Rain03
  • 41
  • 1
  • 7
  • Call your TokenService.validateToken() in your REST controller (endpoint). No need to go out from your webapp to come back into it through another endpoint. – Olivier Depriester Jan 07 '22 at 18:03
  • I forgot to mention, they are in separate modules – Rain03 Jan 07 '22 at 18:04
  • And no common module to share this token validation feature between both endpoints ? If you don't have any, you will surely need to use a http client from your REST controller to send a SOAP request to your SOAP endpoint and using localhost as target host (http client such as ApacheHttpClient or Resteasy, whatever ...) – Olivier Depriester Jan 07 '22 at 18:09
  • I'm a beginner, I don't really know what you meant, but the idea is: I have an /login endpoint where I login with an username and password, as a response I receive the token. All endpoints are available if the request contain this token, for every endpoint I have to call this /validateToken for validating the token – Rain03 Jan 07 '22 at 18:16
  • 2
    What you describe is really unclear. As I understand it you have 2 applications, 1 with a SOAP endpoint and 1 with a REST endpoint. So when your REST API is called, you must send a request to `http://my-soap-endpoint/validateToken` with the token to validate as a request body and then analyze the response to check if your token is actually valid. To send this request to the SOAP endpoint, your REST API code must use a HTTP client. You can find solutions by searching on "how to call a SOAP WS in java" – Olivier Depriester Jan 07 '22 at 19:38
  • Yes, I saw a lot of examples with this style of sending: https://stackoverflow.com/questions/15940234/how-to-do-a-soap-web-service-call-from-java-class. But, I have 3 applications, another REST application which is calling this REST endpoint and I used RestTemplate to call endpoints and get them into JSONObjects. I thought something like RestTemplate would be also in SOAP. – Rain03 Jan 07 '22 at 20:17
  • As its name states it RestTemplate is to be used when using the REST protocol. For SOAP you will have to use another client – Olivier Depriester Jan 07 '22 at 20:20
  • I have to agree with @OlivierDepriester on this, it's unclear. Non necessarily the setup, but the fact that you are mixing SOAP and REST for some services that you seem to own and you are currently writing. Do you own the SOAP service or is it out of your control and you are just forced to call it? If you do not own the SOAP service, is your question simply "how do I write a SOAP client"? – Bogdan Jan 07 '22 at 21:01

0 Answers0