Search in sources :

Example 6 with Base64.encodeBase64String

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));
}
Also used : HttpsBatchMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) HttpsSingleMessage(com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage) Test(org.junit.Test)

Example 7 with Base64.encodeBase64String

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());
}
Also used : SignRequest(com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) NonStrictExpectations(mockit.NonStrictExpectations) Test(org.junit.Test)

Example 8 with Base64.encodeBase64String

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;
}
Also used : Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String)

Example 9 with Base64.encodeBase64String

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);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Base64.encodeBase64String

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('}');
}
Also used : HashMap(java.util.HashMap) MessageProperty(com.microsoft.azure.sdk.iot.device.MessageProperty) Base64.encodeBase64String(org.apache.commons.codec.binary.Base64.encodeBase64String)

Aggregations

Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)14 Test (org.junit.Test)8 Mac (javax.crypto.Mac)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 ProvisioningConnectionString (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningConnectionString)3 ProvisioningSasToken (com.microsoft.azure.sdk.iot.provisioning.service.auth.ProvisioningSasToken)3 IotHubConnectionString (com.microsoft.azure.sdk.iot.service.IotHubConnectionString)3 IotHubServiceSasToken (com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken)2 URLEncoder (java.net.URLEncoder)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Expectations (mockit.Expectations)2 NonStrictExpectations (mockit.NonStrictExpectations)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 MessageProperty (com.microsoft.azure.sdk.iot.device.MessageProperty)1 SignRequest (com.microsoft.azure.sdk.iot.device.hsm.parser.SignRequest)1 HttpsBatchMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsBatchMessage)1 HttpsSingleMessage (com.microsoft.azure.sdk.iot.device.transport.https.HttpsSingleMessage)1 UrlPathBuilder (com.microsoft.azure.sdk.iot.provisioning.device.internal.contract.UrlPathBuilder)1