Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move method IsResourceType from Utils to ResourceTokenAuthorizationHelper #409

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,6 @@ public HashIndex(String jsonString) {
}
}

/**
* Gets data type.
*
* @return the data type.
*/
public DataType getDataType() {
DataType result = null;
try {
result = DataType.valueOf(WordUtils.capitalize(super.getString(Constants.Properties.DATA_TYPE)));
} catch (IllegalArgumentException e) {
// Ignore exception and let the caller handle null value.
this.getLogger().warn("Invalid index dataType value {}.", super.getString(Constants.Properties.DATA_TYPE));
}
return result;
}

/**
* Sets data type.
*
* @param dataType the data type.
*/
public void setDataType(DataType dataType) {
super.set(Constants.Properties.DATA_TYPE, dataType.name());
}

/**
* Gets precision.
*
Expand Down
24 changes: 24 additions & 0 deletions commons/src/main/java/com/microsoft/azure/cosmosdb/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ public IndexKind getKind() {
private void setKind(IndexKind indexKind) {
super.set(Constants.Properties.INDEX_KIND, indexKind.name());
}

/**
* Gets data type.
*
* @return the data type.
*/
public DataType getDataType() {
DataType result = null;
try {
result = DataType.valueOf(WordUtils.capitalize(super.getString(Constants.Properties.DATA_TYPE)));
} catch (IllegalArgumentException e) {
this.getLogger().warn("Invalid index dataType value {}.", super.getString(Constants.Properties.DATA_TYPE));
}
return result;
}

/**
* Sets data type.
*
* @param dataType the data type.
*/
public void setDataType(DataType dataType) {
super.set(Constants.Properties.DATA_TYPE, dataType.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,30 +81,6 @@ public RangeIndex(String jsonString) {
}
}

/**
* Gets data type.
*
* @return the data type.
*/
public DataType getDataType() {
DataType result = null;
try {
result = DataType.valueOf(WordUtils.capitalize(super.getString(Constants.Properties.DATA_TYPE)));
} catch (IllegalArgumentException e) {
this.getLogger().warn("Invalid index dataType value {}.", super.getString(Constants.Properties.DATA_TYPE));
}
return result;
}

/**
* Sets data type.
*
* @param dataType the data type.
*/
public void setDataType(DataType dataType) {
super.set(Constants.Properties.DATA_TYPE, dataType.name());
}

/**
* Gets precision.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,4 @@ public SpatialIndex(String jsonString) {
}
}

/**
* Gets data type.
*
* @return the data type.
*/
public DataType getDataType() {
DataType result = null;
try {
result = DataType.valueOf(WordUtils.capitalize(super.getString(Constants.Properties.DATA_TYPE)));
} catch (IllegalArgumentException e) {
this.getLogger().warn("Invalid index dataType value {}.", super.getString(Constants.Properties.DATA_TYPE));
}
return result;
}

/**
* Sets data type.
*
* @param dataType the data type.
*/
public void setDataType(DataType dataType) {
super.set(Constants.Properties.DATA_TYPE, dataType.name());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -211,4 +212,34 @@ public static String getAuthorizationTokenUsingResourceTokens(
return resourceToken;
}
}

/**
* Checks whether the specified path segment is a resource type
*
* @param resourcePathSegment the path segment to analyze.
* @return true or false
*/
public static boolean IsResourceType(String resourcePathSegment) {
if (StringUtils.isEmpty(resourcePathSegment)) {
return false;
}

switch (resourcePathSegment.toLowerCase()) {
case Paths.ATTACHMENTS_PATH_SEGMENT:
case Paths.COLLECTIONS_PATH_SEGMENT:
case Paths.DATABASES_PATH_SEGMENT:
case Paths.PERMISSIONS_PATH_SEGMENT:
case Paths.USERS_PATH_SEGMENT:
case Paths.DOCUMENTS_PATH_SEGMENT:
case Paths.STORED_PROCEDURES_PATH_SEGMENT:
case Paths.TRIGGERS_PATH_SEGMENT:
case Paths.USER_DEFINED_FUNCTIONS_PATH_SEGMENT:
case Paths.CONFLICTS_PATH_SEGMENT:
case Paths.PARTITION_KEY_RANGES_PATH_SEGMENT:
return true;

default:
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,35 +175,7 @@ public static boolean isDatabaseLink(String link) {
return true;
}

/**
* Checks whether the specified path segment is a resource type
*
* @param resourcePathSegment the path segment to analyze.
* @return true or false
*/
public static boolean IsResourceType(String resourcePathSegment) {
if (StringUtils.isEmpty(resourcePathSegment)) {
return false;
}

switch (resourcePathSegment.toLowerCase()) {
case Paths.ATTACHMENTS_PATH_SEGMENT:
case Paths.COLLECTIONS_PATH_SEGMENT:
case Paths.DATABASES_PATH_SEGMENT:
case Paths.PERMISSIONS_PATH_SEGMENT:
case Paths.USERS_PATH_SEGMENT:
case Paths.DOCUMENTS_PATH_SEGMENT:
case Paths.STORED_PROCEDURES_PATH_SEGMENT:
case Paths.TRIGGERS_PATH_SEGMENT:
case Paths.USER_DEFINED_FUNCTIONS_PATH_SEGMENT:
case Paths.CONFLICTS_PATH_SEGMENT:
case Paths.PARTITION_KEY_RANGES_PATH_SEGMENT:
return true;

default:
return false;
}
}

/**
* Joins the specified paths by appropriately padding them with '/'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@

import com.microsoft.azure.cosmosdb.ConsistencyLevel;
import com.microsoft.azure.cosmosdb.DocumentClientException;
import com.microsoft.azure.cosmosdb.internal.HttpConstants;
import com.microsoft.azure.cosmosdb.internal.ISessionToken;
import com.microsoft.azure.cosmosdb.internal.InternalServerErrorException;
import com.microsoft.azure.cosmosdb.internal.OperationType;
import com.microsoft.azure.cosmosdb.internal.ResourceType;
import com.microsoft.azure.cosmosdb.internal.SessionContainer;
import com.microsoft.azure.cosmosdb.internal.SessionTokenHelper;
import com.microsoft.azure.cosmosdb.internal.*;
import com.microsoft.azure.cosmosdb.rx.internal.BackoffRetryUtility;
import com.microsoft.azure.cosmosdb.rx.internal.Configs;
import com.microsoft.azure.cosmosdb.rx.internal.Exceptions;
Expand Down Expand Up @@ -199,7 +193,7 @@ private void updateResponseHeader(RxDocumentServiceRequest request, Map<String,
ISessionToken sessionToken = null;
String sessionTokenResponseHeader = headers.get(HttpConstants.HttpHeaders.SESSION_TOKEN);
if (!Strings.isNullOrEmpty(sessionTokenResponseHeader)) {
sessionToken = SessionTokenHelper.parse(sessionTokenResponseHeader);
sessionToken = SessionTokenParser.parse(sessionTokenResponseHeader);
}

if (sessionToken != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,7 @@
import com.microsoft.azure.cosmosdb.ClientSideRequestStatistics;
import com.microsoft.azure.cosmosdb.DocumentClientException;
import com.microsoft.azure.cosmosdb.ISessionContainer;
import com.microsoft.azure.cosmosdb.internal.HttpConstants;
import com.microsoft.azure.cosmosdb.internal.ISessionToken;
import com.microsoft.azure.cosmosdb.internal.Integers;
import com.microsoft.azure.cosmosdb.internal.InternalServerErrorException;
import com.microsoft.azure.cosmosdb.internal.MutableVolatile;
import com.microsoft.azure.cosmosdb.internal.OperationType;
import com.microsoft.azure.cosmosdb.internal.SessionTokenHelper;
import com.microsoft.azure.cosmosdb.internal.*;
import com.microsoft.azure.cosmosdb.rx.internal.BadRequestException;
import com.microsoft.azure.cosmosdb.rx.internal.PartitionIsMigratingException;
import com.microsoft.azure.cosmosdb.rx.internal.PartitionKeyRangeIsSplittingException;
Expand Down Expand Up @@ -732,7 +726,7 @@ StoreResult createStoreResult(StoreResponse storeResponse,
// Session token response header is introduced from version HttpConstants.Versions.v2018_06_18 onwards.
// Previously it was only a request header
if ((headerValue = storeResponse.getHeaderValue(HttpConstants.HttpHeaders.SESSION_TOKEN)) != null) {
sessionToken = SessionTokenHelper.parse(headerValue);
sessionToken = SessionTokenParser.parse(headerValue);
}

return new StoreResult(
Expand Down Expand Up @@ -806,7 +800,7 @@ StoreResult createStoreResult(StoreResponse storeResponse,
// Previously it was only a request header
headerValue = documentClientException.getResponseHeaders().get(HttpConstants.HttpHeaders.SESSION_TOKEN);
if (!Strings.isNullOrEmpty(headerValue)) {
sessionToken = SessionTokenHelper.parse(headerValue);
sessionToken = SessionTokenParser.parse(headerValue);
}

return new StoreResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ private void setSessionToken(ResourceId resourceId, String collectionName, Strin

String[] tokenParts = StringUtils.split(token, ':');
partitionKeyRangeId = tokenParts[0];
parsedSessionToken = SessionTokenHelper.parse(tokenParts[1]);
parsedSessionToken = SessionTokenParser.parse(tokenParts[1]);

logger.trace("Update Session token {} {} {}", resourceId.getUniqueDocumentCollectionId(), collectionName, parsedSessionToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static ISessionToken getLocalSessionToken(
throw new BadRequestException(String.format(RMResources.InvalidSessionToken, partitionKeyRangeId));
}

ISessionToken parsedSessionToken = SessionTokenHelper.parse(items[1]);
ISessionToken parsedSessionToken = SessionTokenParser.parse(items[1]);

if (partitionKeyRangeSet.contains(items[0])) {

Expand Down Expand Up @@ -156,26 +156,6 @@ static ISessionToken resolvePartitionLocalSessionToken(RxDocumentServiceRequest
return null;
}

public static ISessionToken parse(String sessionToken) {
ValueHolder<ISessionToken> partitionKeyRangeSessionToken = ValueHolder.initialize(null);

if (SessionTokenHelper.tryParse(sessionToken, partitionKeyRangeSessionToken)) {
return partitionKeyRangeSessionToken.v;
} else {
throw new RuntimeException(new BadRequestException(String.format(RMResources.InvalidSessionToken, sessionToken)));
}
}

static boolean tryParse(String sessionToken, ValueHolder<ISessionToken> parsedSessionToken) {
parsedSessionToken.v = null;
if (!Strings.isNullOrEmpty(sessionToken)) {
String[] sessionTokenSegments = StringUtils.split(sessionToken,":");
return VectorSessionToken.tryCreate(sessionTokenSegments[sessionTokenSegments.length - 1], parsedSessionToken);
} else {
return false;
}
}

public static void validateAndRemoveSessionToken(RxDocumentServiceRequest request) throws DocumentClientException {
String sessionToken = request.getHeaders().get(HttpConstants.HttpHeaders.SESSION_TOKEN);
if (!Strings.isNullOrEmpty(sessionToken)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.microsoft.azure.cosmosdb.internal;

import com.microsoft.azure.cosmosdb.rx.internal.BadRequestException;
import com.microsoft.azure.cosmosdb.rx.internal.RMResources;
import com.microsoft.azure.cosmosdb.rx.internal.Strings;
import com.microsoft.azure.cosmosdb.rx.internal.Utils;
import org.apache.commons.lang3.StringUtils;

public class SessionTokenParser {

public static ISessionToken parse(String sessionToken) {
Utils.ValueHolder<ISessionToken> partitionKeyRangeSessionToken = Utils.ValueHolder.initialize(null);

if (SessionTokenParser.tryParse(sessionToken, partitionKeyRangeSessionToken)) {
return partitionKeyRangeSessionToken.v;
} else {
throw new RuntimeException(new BadRequestException(String.format(RMResources.InvalidSessionToken, sessionToken)));
}
}

static boolean tryParse(String sessionToken, Utils.ValueHolder<ISessionToken> parsedSessionToken) {
parsedSessionToken.v = null;
if (!Strings.isNullOrEmpty(sessionToken)) {
String[] sessionTokenSegments = StringUtils.split(sessionToken,":");
return VectorSessionToken.tryCreate(sessionTokenSegments[sessionTokenSegments.length - 1], parsedSessionToken);
} else {
return false;
}
}


}
Loading