use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessageTest method addMessageEncodesBodyCorrectly.
// Tests_SRS_HTTPSBATCHMESSAGE_11_002: [The function shall add the message as a JSON object appended to the current JSON array.]
// Tests_SRS_HTTPSBATCHMESSAGE_11_003: [The JSON object shall have the field "body" set to the raw message encoded in Base64.]
@Test
public void addMessageEncodesBodyCorrectly(@Mocked final HttpsSingleMessage mockMsg) throws IotHubSizeExceededException {
final String msgBody = "test-msg-body";
new NonStrictExpectations() {
{
mockMsg.getBody();
result = msgBody.getBytes(StandardCharsets.UTF_8);
}
};
List<HttpsSingleMessage> mockMessageList = new ArrayList<>();
mockMessageList.add(mockMsg);
HttpsBatchMessage batchMsg = new HttpsBatchMessage(mockMessageList);
String testBatchBody = new String(batchMsg.getBody(), UTF8).replaceAll("\\s", "");
final String expectedMsgBody = encodeBase64String(msgBody.getBytes(StandardCharsets.UTF_8));
assertThat(testBatchBody, containsString(expectedMsgBody));
}
use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class SignRequestTest method gettersAndSettersWork.
// Tests_SRS_HTTPHSMSIGNREQUEST_34_001: [This function shall save the provided keyId.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_002: [This function shall return the saved data.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_003: [This function shall save the provided data after base64 encoding it.]
// Tests_SRS_HTTPHSMSIGNREQUEST_34_004: [This function shall save the provided algo.]
@Test
public void gettersAndSettersWork() {
// arrange
final String expectedAlgoString = "some algorithm";
final String expectedKeyId = "some key id";
final byte[] expectedData = "some data".getBytes(StandardCharsets.UTF_8);
final String expectedEncodedData = encodeBase64String(expectedData);
new NonStrictExpectations() {
{
mockedMac.getAlgorithm();
result = expectedAlgoString;
}
};
SignRequest request = new SignRequest();
// act
request.setAlgo(mockedMac);
request.setKeyId(expectedKeyId);
request.setData(expectedData);
// assert
assertEquals(mockedMac, Deencapsulation.getField(request, "algo"));
assertEquals(expectedKeyId, Deencapsulation.getField(request, "keyId"));
assertArrayEquals(expectedEncodedData.getBytes(StandardCharsets.UTF_8), request.getData());
}
use of com.google.api.client.util.Base64.encodeBase64String in project gocd by gocd.
the class AgentRegistrationController method getAgentExtraProperties.
private String getAgentExtraProperties() {
if (agentExtraProperties == null) {
String base64OfSystemProperty = encodeBase64String(systemEnvironment.get(AGENT_EXTRA_PROPERTIES).getBytes(UTF_8));
String base64OfEmptyString = encodeBase64String("".getBytes(UTF_8));
this.agentExtraProperties = base64OfSystemProperty.length() >= MAX_HEADER_LENGTH ? base64OfEmptyString : base64OfSystemProperty;
}
return agentExtraProperties;
}
use of com.google.api.client.util.Base64.encodeBase64String in project gocd by gocd.
the class AgentRegistrationController method getToken.
@RequestMapping(value = "/admin/agent/token", method = RequestMethod.GET)
public ResponseEntity getToken(@RequestParam("uuid") String uuid) {
if (StringUtils.isBlank(uuid)) {
String message = "UUID cannot be blank.";
LOG.error("Rejecting request for token. Error: HttpCode=[{}] Message=[{}] UUID=[{}]", CONFLICT, message, uuid);
return new ResponseEntity<>(message, CONFLICT);
}
final AgentInstance agentInstance = agentService.findAgent(uuid);
if ((!agentInstance.isNullAgent() && agentInstance.isPending()) || agentService.isRegistered(uuid)) {
String message = "A token has already been issued for this agent.";
LOG.error("Rejecting request for token. Error: HttpCode=[{}] Message=[{}] Pending=[{}] UUID=[{}]", CONFLICT, message, agentInstance.isPending(), uuid);
return new ResponseEntity<>(message, CONFLICT);
}
String token;
synchronized (HMAC_GENERATION_MUTEX) {
token = encodeBase64String(hmac().doFinal(uuid.getBytes()));
}
return new ResponseEntity<>(token, OK);
}
use of com.google.api.client.util.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.
the class HttpsBatchMessage method addJsonToStringBuilder.
/**
* Converts a service-bound message to a JSON object with the correct
* format.
*
* @param msg the message to be converted to a corresponding JSON object.
*
* @return the JSON string representation of the message.
*/
private static void addJsonToStringBuilder(HttpsSingleMessage msg, StringBuilder jsonStringBuilder) {
jsonStringBuilder.append('{' + BODY + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append('\"').append(encodeBase64String(msg.getBody())).append("\",");
jsonStringBuilder.append(BASE_ENCODED_KEY + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append(true);
MessageProperty[] properties = msg.getProperties();
Map<String, String> allProperties = new HashMap<>(msg.getSystemProperties());
for (MessageProperty p : properties) {
allProperties.put(p.getName(), p.getValue());
}
int numProperties = allProperties.size();
if (numProperties > 0) {
jsonStringBuilder.append(',');
jsonStringBuilder.append(PROPERTIES + KEY_VALUE_SEPARATOR);
jsonStringBuilder.append('{');
for (String key : allProperties.keySet()) {
jsonStringBuilder.append('\"').append(key).append("\":");
jsonStringBuilder.append('\"').append(allProperties.get(key)).append("\",");
}
// remove last trailing comma
jsonStringBuilder.deleteCharAt(jsonStringBuilder.length() - 1);
jsonStringBuilder.append('}');
}
jsonStringBuilder.append('}');
}
Aggregations