Class CosmosAuthorizationHeader
- java.lang.Object
-
- com.adaptris.core.ServiceImp
-
- com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeaderImpl
-
- com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeader
-
- All Implemented Interfaces:
AdaptrisComponent
,ComponentLifecycle
,ComponentLifecycleExtension
,MessageEventGenerator
,Service
,StateManagedComponent
@ComponentProfile(summary="Builds an authorization header for Azure CosmosDB", since="3.9.2", tag="azure,cosmosdb,cosmos") public class CosmosAuthorizationHeader extends CosmosAuthorizationHeaderImpl
Builds an authorization header for Azure CosmosDB from an explicit ResourceID / ResourceTypeBuilds an authorization header using master keys based on the CosmosDB documentation. Once the key is built, then you can send it as your
Authorization
header. Under the covers we actually usecom.microsoft.azure.documentdb.internal.BaseAuthorizationTokenProvider
from the Cosmos Java SDK; but effectively what we do is this (you could do this in anEmbeddedScriptingService
or similar).verb = "GET"; resourceType = "the resource type"; resourceId = "the resource id"; // From java.time date = DateTimeFormatter.RFC_1123_DATE_TIME.format(ZonedDateTime.now(ZoneId.of("GMT")); key = "The key"; keyType = "master"; tokenVersion = "1.0"; stringToSign = verb.toLowerCase() + "\n" + resourceType.toLowerCase() + "\n" + resourceId + "\n" + date.toLowerCase() + "\n" + "" + "\n"; // From org.apache.commons.codec, unbase64 the key, and use it to hmac, then back to base64 again. sigBytes = new HmacUtils(HmacAlgorithms.HMAC_SHA_256, Base64.decodeBase64(key)) .hmac(stringToSign.getBytes("UTF-8")); signature = Base64.encodeBase64String(sigBytes); // from java.net authorization = URLEncoder.encode("type=" + keyType + "&ver=" + tokenVersion + "&sig=" + signature, "UTF-8");
Since the date is also part of the signed data, it will also add the date it used against the key
"x-ms-date"
so that you can send it as a HTTP request header.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_DATE_FORMAT
The default date format: "EEE, dd MMM yyyy HH:mm:ss z".static java.lang.String
DEFAULT_TIMEZONE
-
Fields inherited from class com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeaderImpl
DEFAULT_METADATA_KEY, X_MS_DATE
-
Fields inherited from class com.adaptris.core.ServiceImp
log
-
-
Constructor Summary
Constructors Constructor Description CosmosAuthorizationHeader()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
doService(AdaptrisMessage msg)
java.lang.String
getDateFormat()
Override the default date format.java.lang.String
getResourceId()
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive.@NonNull java.lang.String
getResourceType()
The ResourceType portion of the string identifies the type of resource that the request is for, Eg.java.lang.String
getTimezone()
Set the timezone as required.void
prepare()
void
setDateFormat(java.lang.String dateFormat)
Override the default date format.void
setResourceId(java.lang.String resourceId)
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive.void
setResourceType(@NonNull java.lang.String resourceType)
The ResourceType portion of the string identifies the type of resource that the request is for, Eg.void
setTimezone(java.lang.String timezone)
Set the timezone as required.CosmosAuthorizationHeader
withResourceId(java.lang.String s)
CosmosAuthorizationHeader
withResourceType(java.lang.String s)
-
Methods inherited from class com.adaptris.interlok.azure.cosmosdb.CosmosAuthorizationHeaderImpl
closeService, getHttpVerb, getMasterKey, getTargetKey, initService, setHttpVerb, setMasterKey, setTargetKey, targetKey, withHttpVerb, withMasterKey, withTargetKey
-
Methods inherited from class com.adaptris.core.ServiceImp
changeState, close, continueOnFailure, createName, createQualifier, getContinueOnFail, getIsTrackingEndpoint, getUniqueId, init, isBranching, isTrackingEndpoint, requestClose, requestInit, requestStart, requestStop, retrieveComponentState, setContinueOnFail, setIsTrackingEndpoint, setUniqueId, start, stop
-
-
-
-
Field Detail
-
DEFAULT_DATE_FORMAT
public static final java.lang.String DEFAULT_DATE_FORMAT
The default date format: "EEE, dd MMM yyyy HH:mm:ss z".- See Also:
- Constant Field Values
-
DEFAULT_TIMEZONE
public static final java.lang.String DEFAULT_TIMEZONE
- See Also:
- Constant Field Values
-
-
Method Detail
-
prepare
public void prepare() throws CoreException
- Specified by:
prepare
in interfaceComponentLifecycleExtension
- Overrides:
prepare
in classCosmosAuthorizationHeaderImpl
- Throws:
CoreException
-
doService
public void doService(AdaptrisMessage msg) throws ServiceException
- Throws:
ServiceException
-
withResourceId
public CosmosAuthorizationHeader withResourceId(java.lang.String s)
-
withResourceType
public CosmosAuthorizationHeader withResourceType(java.lang.String s)
-
getResourceType
@NonNull public @NonNull java.lang.String getResourceType()
The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
-
setResourceType
public void setResourceType(@NonNull @NonNull java.lang.String resourceType)
The ResourceType portion of the string identifies the type of resource that the request is for, Eg. "dbs", "colls", "docs".
-
getResourceId
public java.lang.String getResourceId()
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive. For example, a collection it might look like: "dbs/MyDatabase/colls/MyCollection".
-
setResourceId
public void setResourceId(java.lang.String resourceId)
The ResourceLink portion of the string is the identity property of the resource that the request is directed at and is case sensitive. For example, a collection it might look like: "dbs/MyDatabase/colls/MyCollection".
-
getTimezone
public java.lang.String getTimezone()
Set the timezone as required.By default it is set to GMT, as this is the expected date style for CosmosDB. You shouldn't need to change it.
-
setTimezone
public void setTimezone(java.lang.String timezone)
Set the timezone as required.By default it is set to GMT, as this is the expected date style for CosmosDB. You shouldn't need to change it.
-
getDateFormat
public java.lang.String getDateFormat()
Override the default date format.By default it is set to be "EEE, dd MMM yyyy HH:mm:ss z" which imposes a leading digit on the day if required;
DateTimeFormatter.RFC_1123_DATE_TIME
doesn't appear suitable for use with Azure CosmosDB as it doesn't provide the leading digit.
-
setDateFormat
public void setDateFormat(java.lang.String dateFormat)
Override the default date format.By default it is set to be "EEE, dd MMM yyyy HH:mm:ss z" which imposes a leading digit on the day if required;
DateTimeFormatter.RFC_1123_DATE_TIME
doesn't appear suitable for use with Azure CosmosDB as it doesn't provide the leading digit.
-
-