Skip to content

Commit

Permalink
feat(prov-dev, deps): Add support for retry-after headers from DPS
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrando authored and timtay-microsoft committed May 31, 2019
1 parent d50d831 commit bd1ec4b
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,23 @@ public void setApplicationProperty(Map<String, Object> userProperties)
this.messageImpl.setApplicationProperties(applicationProperties);
}

/**
* Set the application property for the message
* @return Map of properties
*/
public Map<String, Object> getApplicationProperty()
{
ApplicationProperties appProperty = this.messageImpl.getApplicationProperties();
if (appProperty == null)
{
return null;
}
else
{
return appProperty.getValue();
}
}

/**
* Sets the data value
* @param data the {@code byte[]} to be decoded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public String getHeaderField(String field)
return values;
}

public boolean isFieldAvailable(String field)
{
String canonicalizedField = canonicalizeFieldName(field);
return this.headerFields.containsKey(canonicalizedField);
}

/**
* Getter for the header fields.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@

public abstract class ProvisioningDeviceClientContract
{
private int retryValue = DEFAULT_RETRY_AFTER_VALUE;
protected static final String RETRY_AFTER = "retry-after";
private static final Integer DEFAULT_RETRY_AFTER_VALUE = 2;
private static final Integer MAX_PROV_GET_THROTTLE_TIME = 5;

protected void setRetrieveRetryAfterValue(String protocolRetryValue)
{
if (protocolRetryValue != null && !protocolRetryValue.isEmpty())
{
retryValue = Integer.parseInt(protocolRetryValue);
// ensure the value is between the tolerances
if (retryValue < DEFAULT_RETRY_AFTER_VALUE || retryValue > MAX_PROV_GET_THROTTLE_TIME)
{
retryValue = DEFAULT_RETRY_AFTER_VALUE;
}
}
}

/**
* Static method to create contracts with the service over the specified protocol
* @param provisioningDeviceClientConfig Config used for provisioning
Expand Down Expand Up @@ -58,4 +76,13 @@ public static ProvisioningDeviceClientContract createProvisioningContract(Provis
public abstract void authenticateWithProvisioningService(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException;
public abstract void getRegistrationStatus(RequestData requestData, ResponseCallback responseCallback, Object dpsAuthorizationCallbackContext) throws ProvisioningDeviceClientException;
public abstract void close() throws ProvisioningDeviceConnectionException;

/**
* Method to get the DPS retry after value
* @return integer value of the number of milliseconds to wait to call dps service
*/
public int getRetryValue()
{
return this.retryValue*1000;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.util.Map;

public class ContractAPIAmqp extends ProvisioningDeviceClientContract
{
Expand All @@ -27,6 +28,18 @@ public class ContractAPIAmqp extends ProvisioningDeviceClientContract
private String idScope;
private SaslHandler amqpSaslHandler;

private void processRetryAfterValue(Map<String, Object> appProperties)
{
if (appProperties != null)
{
if (appProperties.containsKey(RETRY_AFTER))
{
Object retryAfterValue = appProperties.get(RETRY_AFTER);
setRetrieveRetryAfterValue(retryAfterValue.toString());
}
}
}

/**
* This constructor creates an instance of DpsAPIAmqps class and initializes member variables
* @param provisioningDeviceClientConfig Config used for provisioning Cannot be {@code null}.
Expand Down Expand Up @@ -173,6 +186,8 @@ public synchronized void authenticateWithProvisioningService(RequestData request

// SRS_ContractAPIAmqp_07_005: [This method shall send an AMQP message with the property of iotdps-register.]
this.provisioningAmqpOperations.sendRegisterMessage(responseCallback, callbackContext, payload);

processRetryAfterValue(this.provisioningAmqpOperations.getAmqpMessageProperties());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class ProvisioningAmqpOperations extends AmqpDeviceOperations implements
private final Queue<AmqpMessage> receivedMessages = new LinkedBlockingQueue<>();
private final ObjectLock receiveLock = new ObjectLock();

private Map<String, Object> messageAppProperties;
private String idScope;
private String hostName;

Expand Down Expand Up @@ -94,6 +95,9 @@ private void retrieveAmqpMessage(ResponseCallback responseCallback, Object callb
if (this.receivedMessages.size() > 0)
{
AmqpMessage message = this.receivedMessages.remove();

// Need to keep property around to get the retry-after value
this.messageAppProperties = message.getApplicationProperty();
byte[] msgData = message.getAmqpBody();
if (msgData != null)
{
Expand Down Expand Up @@ -286,6 +290,15 @@ public void sendRegisterMessage(ResponseCallback responseCallback, Object callba
}
}

/**
* Returns the message properties of the current message
* @return Map of application properties
*/
public Map<String, Object> getAmqpMessageProperties()
{
return this.messageAppProperties;
}

/**
* connectionEstablished Unused
*/
Expand Down
Loading

0 comments on commit bd1ec4b

Please sign in to comment.