use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class PolicyDAOImpl method setHeaderConditions.
/**
* Add Header conditions of pipeline with pipeline Id: <code>pipelineId</code> to a
* provided {@link Condition} array
*
* @param connection Connection of the database
* @param pipelineId Id of the pipeline
* @param conditions condition array to populate
* @throws SQLException
*/
private void setHeaderConditions(int pipelineId, ArrayList<Condition> conditions, Connection connection) throws SQLException {
final String query = "SELECT " + "HEADER_FIELD_NAME, " + "HEADER_FIELD_VALUE , IS_HEADER_FIELD_MAPPING " + " FROM " + "AM_HEADER_FIELD_CONDITION " + "WHERE " + "CONDITION_GROUP_ID =?";
try (PreparedStatement preparedStatement = connection.prepareStatement(query)) {
preparedStatement.setInt(1, pipelineId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
HeaderCondition headerCondition = new HeaderCondition();
headerCondition.setHeader(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_NAME));
headerCondition.setValue(resultSet.getString(APIMgtConstants.ThrottlePolicyConstants.COLUMN_HEADER_FIELD_VALUE));
headerCondition.setInvertCondition(resultSet.getBoolean(APIMgtConstants.ThrottlePolicyConstants.COLUMN_IS_HEADER_FIELD_MAPPING));
conditions.add(headerCondition);
}
}
}
}
use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class RestCallUtilImpl method postRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse postRequest(URI uri, MediaType acceptContentType, List<String> cookies, Entity entity, MediaType payloadContentType, Map<String, String> headers) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
if (entity == null) {
throw new IllegalArgumentException("Entity must not be null");
}
if (payloadContentType == null) {
throw new IllegalArgumentException("Payload content type must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, payloadContentType.toString());
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
if (cookies != null && !cookies.isEmpty()) {
for (String cookie : cookies) {
httpConnection.addRequestProperty(APIMgtConstants.FunctionsConstants.COOKIE, cookie.split(";", 2)[0]);
}
}
if (headers != null) {
for (Map.Entry<String, String> header : headers.entrySet()) {
httpConnection.addRequestProperty(header.getKey(), header.getValue());
}
}
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(entity.getEntity().toString().getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.messaging.Header 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();
}
}
use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class PoliciesApiServiceImpl method policiesThrottlingCustomGet.
/**
* Retrieves all custom policies.
*
* @param ifNoneMatch If-None-Match header value
* @param ifModifiedSince If-Modified-Since header value
* @param request msf4j request object
* @return All matched Global Throttle policies to the given request
* @throws NotFoundException if an error occurred when particular resource does not exits in the system.
*/
@Override
public Response policiesThrottlingCustomGet(String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
if (log.isDebugEnabled()) {
log.debug("Received Custom Policy GET request.");
}
try {
APIMgtAdminService apiMgtAdminService = RestApiUtil.getAPIMgtAdminService();
List<CustomPolicy> policies = apiMgtAdminService.getCustomRules();
CustomRuleListDTO customRuleListDTO = CustomPolicyMappingUtil.fromCustomPolicyArrayToListDTO(policies);
return Response.ok().entity(customRuleListDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error occurred while retrieving custom policies";
org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.
the class CommonThrottleMappingUtil method fromDTOToHeaderCondition.
/**
* Converts a Header Condition DTO object into a model object
*
* @param dto Header Condition DTO object
* @return Header Condition model object derived from Header Condition DTO
*/
public static HeaderCondition fromDTOToHeaderCondition(ThrottleConditionDTO dto) {
HeaderCondition headerCondition = new HeaderCondition();
headerCondition = updateFieldsFromDTOToCondition(dto, headerCondition);
headerCondition.setHeader(dto.getHeaderCondition().getHeaderName());
headerCondition.setValue(dto.getHeaderCondition().getHeaderValue());
return headerCondition;
}
Aggregations