use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class ETagUtils method getHash.
/**
* If some other hashing algorithm is needed use this method.
*
* @param updatedTime, the updated/created time of the resource in UNIX time
* @param algorithm the algorithm used for hashing
* @return String
* @throws NoSuchAlgorithmException if the given algorithm is invalid or not found in {@link MessageDigest}
* @throws ETagGenerationException if hash generation failed.
*/
private static String getHash(String updatedTime, String algorithm) throws ETagGenerationException, NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
try {
messageDigest.update(updatedTime.getBytes("UTF-8"));
byte[] digest = messageDigest.digest();
// conversion to hexadecimal
StringBuilder sb = new StringBuilder();
for (byte b : digest) {
sb.append(String.format("%02x", b & 0xff));
}
String generatedHash = sb.toString();
if (log.isDebugEnabled()) {
log.debug("ETag generated in HEX '" + generatedHash + "' for '" + updatedTime + "'");
}
return sb.toString();
} catch (UnsupportedEncodingException e) {
String errorMessage = "Error while converting timestamp to String :" + updatedTime;
log.error(errorMessage, e);
throw new ETagGenerationException(errorMessage, e);
}
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class ThrottlerUtil method addDefaultAdvancedThrottlePolicies.
/**
* Deploy default throttle polices at startup
*
* @throws APIManagementException throws if any exception occured
*/
public static void addDefaultAdvancedThrottlePolicies() throws APIManagementException {
int[] requestCount = new int[] { 50, 20, 10, Integer.MAX_VALUE };
// Adding application level throttle policies
String[] appPolicies = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_FIFTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TWENTY_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_TEN_REQ_PER_MIN, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED };
String[] appPolicyDecs = new String[] { ThrottleConstants.DEFAULT_APP_POLICY_LARGE_DESC, ThrottleConstants.DEFAULT_APP_POLICY_MEDIUM_DESC, ThrottleConstants.DEFAULT_APP_POLICY_SMALL_DESC, ThrottleConstants.DEFAULT_APP_POLICY_UNLIMITED_DESC };
PolicyDAO policyDAO = DAOFactory.getPolicyDAO();
String policyName;
// Add application level throttle policies
for (int i = 0; i < appPolicies.length; i++) {
policyName = appPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.application, policyName)) {
ApplicationPolicy applicationPolicy = new ApplicationPolicy(policyName);
applicationPolicy.setUuid(UUID.randomUUID().toString());
applicationPolicy.setDisplayName(policyName);
applicationPolicy.setDescription(appPolicyDecs[i]);
applicationPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCount[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
applicationPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApplicationPolicy(applicationPolicy);
}
}
// Adding Subscription level policies
int[] requestCountSubPolicies = new int[] { 5000, 2000, 1000, 500, Integer.MAX_VALUE };
String[] subPolicies = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED };
String[] subPolicyDecs = new String[] { ThrottleConstants.DEFAULT_SUB_POLICY_GOLD_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_SILVER_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_BRONZE_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNAUTHENTICATED_DESC, ThrottleConstants.DEFAULT_SUB_POLICY_UNLIMITED_DESC };
for (int i = 0; i < subPolicies.length; i++) {
policyName = subPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.subscription, policyName)) {
SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName);
subscriptionPolicy.setUuid(UUID.randomUUID().toString());
subscriptionPolicy.setDisplayName(policyName);
subscriptionPolicy.setDescription(subPolicyDecs[i]);
subscriptionPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountSubPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
subscriptionPolicy.setStopOnQuotaReach(true);
subscriptionPolicy.setBillingPlan(ThrottleConstants.BILLING_PLAN_FREE);
policyDAO.addSubscriptionPolicy(subscriptionPolicy);
}
}
// Adding Resource level policies
String[] apiPolicies = new String[] { ThrottleConstants.DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED };
String[] apiPolicyDecs = new String[] { ThrottleConstants.DEFAULT_API_POLICY_ULTIMATE_DESC, ThrottleConstants.DEFAULT_API_POLICY_PLUS_DESC, ThrottleConstants.DEFAULT_API_POLICY_BASIC_DESC, ThrottleConstants.DEFAULT_API_POLICY_UNLIMITED_DESC };
int[] requestCountApiPolicies = new int[] { 50000, 20000, 10000, Integer.MAX_VALUE };
for (int i = 0; i < apiPolicies.length; i++) {
policyName = apiPolicies[i];
if (!isPolicyExist(APIMgtAdminService.PolicyLevel.api, policyName)) {
APIPolicy apiPolicy = new APIPolicy(policyName);
apiPolicy.setUuid(UUID.randomUUID().toString());
apiPolicy.setDisplayName(policyName);
apiPolicy.setDescription(apiPolicyDecs[i]);
apiPolicy.setUserLevel(ThrottleConstants.API_POLICY_API_LEVEL);
apiPolicy.setDeployed(true);
QuotaPolicy defaultQuotaPolicy = new QuotaPolicy();
RequestCountLimit requestCountLimit = new RequestCountLimit(ThrottleConstants.TIME_UNIT_MINUTE, 1, requestCountApiPolicies[i]);
defaultQuotaPolicy.setType(PolicyConstants.REQUEST_COUNT_TYPE);
defaultQuotaPolicy.setLimit(requestCountLimit);
apiPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy);
policyDAO.addApiPolicy(apiPolicy);
}
}
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiDefinitionErrorGettingSwaggerResource.
@Test(description = "Response not 200 when getting swagger resource from url when adding api from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorGettingSwaggerResource() throws APIManagementException, LifecycleException, IOException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
Mockito.when(httpURLConnection.getResponseCode()).thenReturn(400);
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class APIPublisherImplTestCase method testAddApiDefinitionErrorIOException.
@Test(description = "Test IO exception when adding api definition from swagger resource", expectedExceptions = APIManagementException.class)
public void testAddApiDefinitionErrorIOException() throws APIManagementException, LifecycleException, IOException {
ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
HttpURLConnection httpURLConnection = Mockito.mock(HttpURLConnection.class);
APILifecycleManager apiLifecycleManager = Mockito.mock(APILifecycleManager.class);
Mockito.when(apiLifecycleManager.addLifecycle(APIMgtConstants.API_LIFECYCLE, USER)).thenReturn(new LifecycleState());
APIGateway gateway = Mockito.mock(APIGateway.class);
APIPublisherImpl apiPublisher = getApiPublisherImpl(apiDAO, apiLifecycleManager, gateway);
Mockito.doThrow(new IOException()).when(httpURLConnection).getResponseCode();
apiPublisher.addApiFromDefinition(httpURLConnection);
Mockito.verify(apiLifecycleManager, Mockito.times(0)).addLifecycle(APIMgtConstants.API_LIFECYCLE, USER);
}
use of org.wso2.carbon.registry.core.Resource in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingApplicationIdPut.
/**
* Updates/adds a new Application throttle policy to the system
*
* @param id Uuid of the policy.
* @param body DTO object including the Policy meta information
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return Response object response object with the updated application throttle policy resource
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingApplicationIdPut(String id, ApplicationThrottlePolicyDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
APIMgtAdminService.PolicyLevel tierLevel = APIMgtAdminService.PolicyLevel.application;
if (log.isDebugEnabled()) {
log.info("Received Application Policy PUT request " + body + " with tierLevel = " + tierLevel);
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
ApplicationPolicy applicationPolicy = ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyDTOToModel(body);
applicationPolicy.setUuid(id);
apiMgtAdminService.updateApplicationPolicy(applicationPolicy);
return Response.status(Response.Status.OK).entity(ApplicationThrottlePolicyMappingUtil.fromApplicationThrottlePolicyToDTO(apiMgtAdminService.getApplicationPolicyByUuid(id))).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while updating Application Policy. policy uuid: " + id;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
Aggregations