public static JsonNode convertToJsonNode(Object object) throws IOException { if (object instanceof JsonNode) { return (JsonNode) object; } ByteArrayOutputStream buffer = new ByteArrayOutputStream(); buffer.write(JsonSerialization.writeValueAsBytes(object)); return MAPPER.readValue(buffer.toByteArray(), JsonNode.class); }
public String getBase64Header() throws IOException { if (base64Header == null && header != null) { byte[] contentBytes = JsonSerialization.writeValueAsBytes(header); base64Header = Base64Url.encode(contentBytes); } return base64Header; }
public EncodingBuilder jsonContent(Object object) { try { this.contentBytes = JsonSerialization.writeValueAsBytes(object); } catch (IOException e) { throw new RuntimeException(e); } return new EncodingBuilder(); }
public static void doDeleteJSON(String resourceUrl, String auth, Object content) { Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(content); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("delete", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: DELETE " + resourceUrl + "\n" + new String(body), e); } checkSuccess(resourceUrl, response); }
public static void doPostJSON(String resourceUrl, String auth, Object content) { Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(content); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("post", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: POST " + resourceUrl + "\n" + new String(body), e); } checkSuccess(resourceUrl, response); }
public static void doPostJSON(String resourceUrl, String auth, Object content) { Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(content); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("post", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: POST " + resourceUrl + "\n" + new String(body), e); } checkSuccess(resourceUrl, response); }
public static void doDeleteJSON(String resourceUrl, String auth, Object content) { Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(content); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("delete", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: DELETE " + resourceUrl + "\n" + new String(body), e); } checkSuccess(resourceUrl, response); }
/** * Creates an {@link ObjectNode} based on the given {@code pojo}, copying all its properties to the resulting {@link ObjectNode}. * * @param pojo a pojo which properties will be populates into the resulting a {@link ObjectNode} * @return a {@link ObjectNode} with all the properties from the given pojo * @throws IOException if the resulting a {@link ObjectNode} can not be created */ public static ObjectNode createObjectNode(Object pojo) throws IOException { if (pojo == null) { throw new IllegalArgumentException("Pojo can not be null."); } ObjectNode objectNode = createObjectNode(); JsonParser jsonParser = mapper.getJsonFactory().createJsonParser(writeValueAsBytes(pojo)); JsonNode jsonNode = jsonParser.readValueAsTree(); if (!jsonNode.isObject()) { throw new RuntimeException("JsonNode [" + jsonNode + "] is not a object."); } objectNode.putAll((ObjectNode) jsonNode); return objectNode; }
public static void resetUserPassword(String rootUrl, String realm, String auth, String userid, String password, boolean temporary) { String resourceUrl = composeResourceUrl(rootUrl, realm, "users/" + userid + "/reset-password"); Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); CredentialRepresentation credentials = new CredentialRepresentation(); credentials.setType("password"); credentials.setTemporary(temporary); credentials.setValue(password); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(credentials); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("put", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: PUT " + resourceUrl + "\n" + new String(body), e); } response.checkSuccess(); }
public static void resetUserPassword(String rootUrl, String realm, String auth, String userid, String password, boolean temporary) { String resourceUrl = composeResourceUrl(rootUrl, realm, "users/" + userid + "/reset-password"); Headers headers = new Headers(); if (auth != null) { headers.add("Authorization", auth); } headers.add("Content-Type", "application/json"); CredentialRepresentation credentials = new CredentialRepresentation(); credentials.setType("password"); credentials.setTemporary(temporary); credentials.setValue(password); HeadersBodyStatus response; byte[] body; try { body = JsonSerialization.writeValueAsBytes(credentials); } catch (IOException e) { throw new RuntimeException("Failed to serialize JSON", e); } try { response = HttpUtil.doRequest("put", resourceUrl, new HeadersBody(headers, new ByteArrayInputStream(body))); } catch (IOException e) { throw new RuntimeException("HTTP request failed: PUT " + resourceUrl + "\n" + new String(body), e); } response.checkSuccess(); }
@Override public Object call() throws Exception { http.<ResourceRepresentation>put(serverConfiguration.getResourceRegistrationEndpoint() + "/" + resource.getId()) .authorizationBearer(pat.call()) .json(JsonSerialization.writeValueAsBytes(resource)).execute(); return null; } };
@Override public Void call() throws Exception { http.<Void>put(serverConfiguration.getPolicyEndpoint() + "/"+ permission.getId()) .authorizationBearer(pat.call()) .json(JsonSerialization.writeValueAsBytes(permission)).execute(); return null; } };
public static String jweDirectEncode(Key aesKey, Key hmacKey, JsonWebToken jwt) throws JWEException { int keyLength = aesKey.getEncoded().length; String encAlgorithm; switch (keyLength) { case 16: encAlgorithm = JWEConstants.A128CBC_HS256; break; case 24: encAlgorithm = JWEConstants.A192CBC_HS384; break; case 32: encAlgorithm = JWEConstants.A256CBC_HS512; break; default: throw new IllegalArgumentException("Bad size for Encryption key: " + aesKey + ". Valid sizes are 16, 24, 32."); } try { byte[] contentBytes = JsonSerialization.writeValueAsBytes(jwt); JWEHeader jweHeader = new JWEHeader(JWEConstants.DIR, encAlgorithm, null); JWE jwe = new JWE() .header(jweHeader) .content(contentBytes); jwe.getKeyStorage() .setCEKKey(aesKey, JWEKeyStorage.KeyUse.ENCRYPTION) .setCEKKey(hmacKey, JWEKeyStorage.KeyUse.SIGNATURE); return jwe.encodeJwe(); } catch (IOException ioe) { throw new JWEException(ioe); } }
@Override public Object call() throws Exception { http.<List>put(serverConfiguration.getPermissionEndpoint()+"/ticket") .json(JsonSerialization.writeValueAsBytes(ticket)) .authorizationBearer(pat.call()) .response().json(List.class).execute(); return null; } };
@Override public PermissionResponse call() throws Exception { return http.<PermissionResponse>post(serverConfiguration.getPermissionEndpoint()) .authorizationBearer(pat.call()) .json(JsonSerialization.writeValueAsBytes(requests)) .response().json(PermissionResponse.class).execute(); } };
@Override public PermissionTicketRepresentation call() throws Exception { return http.<PermissionTicketRepresentation>post(serverConfiguration.getPermissionEndpoint()+"/ticket") .json(JsonSerialization.writeValueAsBytes(ticket)) .authorizationBearer(pat.call()) .response().json(new TypeReference<PermissionTicketRepresentation>(){}).execute(); } };
@Override public ResourceRepresentation call() throws Exception { return http.<ResourceRepresentation>post(serverConfiguration.getResourceRegistrationEndpoint()) .authorizationBearer(pat.call()) .json(JsonSerialization.writeValueAsBytes(resource)) .response().json(ResourceRepresentation.class).execute(); } };
@Override public UmaPermissionRepresentation call() throws Exception { return http.<UmaPermissionRepresentation>post(serverConfiguration.getPolicyEndpoint() + "/" + resourceId) .authorizationBearer(pat.call()) .json(JsonSerialization.writeValueAsBytes(permission)) .response().json(UmaPermissionRepresentation.class).execute(); } };
authzRequest.setClaimToken(Base64.encodeBytes(JsonSerialization.writeValueAsBytes(claims)));