Skip to content

Instantly share code, notes, and snippets.

@dacrome
Created October 11, 2015 18:51
Show Gist options
  • Save dacrome/ee576463d6fa8426c393 to your computer and use it in GitHub Desktop.
Save dacrome/ee576463d6fa8426c393 to your computer and use it in GitHub Desktop.
ObjectMapper with Jersey Client
public Client updateClient(String updateClientId, Client client, AccessToken accessToken) {
Client updatedClient = null;
try {
updatedClient = targetEndpoint.path(CLIENT_ENDPOINT).path(updateClientId)
.request(MediaType.APPLICATION_JSON)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME, clientId)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD, clientSecret)
.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout)
.property(ClientProperties.READ_TIMEOUT, readTimeout)
.header(AUTHORIZATION_HEADER, BEARER + accessToken.getToken())
.put(Entity.entity(client, MediaType.APPLICATION_JSON), Client.class);
} catch (ProcessingException e) {
throw createGeneralConnectionInitializationException(e);
} catch (WebApplicationException e) {
final Response response = e.getResponse();
checkAndHandleResponse(response.readEntity(String.class), response.getStatusInfo(), accessToken);
}
return updatedClient;
}
public Client updateClient(String updateClientId, Client client, AccessToken accessToken) {
StatusType status;
String clientResponse;
String clientAsString;
try {
clientAsString = OBJECT_MAPPER.writeValueAsString(client);
} catch (JsonProcessingException e) {
throw new OsiamClientException(String.format("Unable to parse Client: %s", client), e);
}
try {
Response response = targetEndpoint.path(CLIENT_ENDPOINT).path(updateClientId)
.request(MediaType.APPLICATION_JSON)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME, clientId)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD, clientSecret)
.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout)
.property(ClientProperties.READ_TIMEOUT, readTimeout)
.header(AUTHORIZATION_HEADER, BEARER + accessToken.getToken())
.put(Entity.entity(clientAsString, MediaType.APPLICATION_JSON));
status = response.getStatusInfo();
clientResponse = response.readEntity(String.class);
} catch (ProcessingException e) {
throw createGeneralConnectionInitializationException(e);
}
checkAndHandleResponse(clientResponse, status, accessToken);
try {
return OBJECT_MAPPER.readValue(clientResponse, Client.class);
} catch (IOException e) {
throw new OsiamClientException(String.format("Unable to parse Client: %s", clientResponse), e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment