use of org.apache.cxf.jaxrs.ext.MessageContext in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method getAdditionalInfoOfAPISubscriptions.
/**
* Get additional Info details of subscriptions attached with given API
*
* @param apiId apiId
* @param offset starting index of the subscription list
* @param limit max num of subscriptions returned
* @param ifNoneMatch If-None-Match header value
* @param messageContext message context
* @return Response with additional Info of the GraphQL API
*/
@Override
public Response getAdditionalInfoOfAPISubscriptions(String apiId, String groupId, String xWSO2Tenant, Integer offset, Integer limit, String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
Subscriber subscriber = new Subscriber(username);
Set<SubscribedAPI> subscriptions;
List<SubscribedAPI> subscribedAPIList = new ArrayList<>();
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
AdditionalSubscriptionInfoListDTO additionalSubscriptionInfoListDTO;
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
if (apiTypeWrapper.isAPIProduct()) {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApiProduct().getId(), groupId, organization);
} else {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApi().getId(), groupId, organization);
}
// Sort subscriptions by application name
subscribedAPIList.addAll(subscriptions);
subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
additionalSubscriptionInfoListDTO = AdditionalSubscriptionInfoMappingUtil.fromAdditionalSubscriptionInfoListToDTO(subscribedAPIList, limit, offset, organization);
AdditionalSubscriptionInfoMappingUtil.setPaginationParams(additionalSubscriptionInfoListDTO, apiId, "", limit, offset, subscribedAPIList.size());
return Response.ok().entity(additionalSubscriptionInfoListDTO).build();
} catch (APIManagementException e) {
// to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving additional information details of the API : " + apiId, e, log);
} else {
String msg = "Error while retrieving additional information details of the API " + apiId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
}
return null;
}
use of org.apache.cxf.jaxrs.ext.MessageContext in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImpl method subscriptionsGet.
/**
* Get all subscriptions that are of user or shared subscriptions of the user's group.
* <p/>
* If apiId is specified this will return the subscribed applications of that api
* If application id is specified this will return the api subscriptions of that application
*
* @param apiId api identifier
* @param applicationId application identifier
* @param offset starting index of the subscription list
* @param limit max num of subscriptions returned
* @param ifNoneMatch If-None-Match header value
* @return matched subscriptions as a list of SubscriptionDTOs
*/
@Override
public Response subscriptionsGet(String apiId, String applicationId, String groupId, String xWSO2Tenant, Integer offset, Integer limit, String ifNoneMatch, MessageContext messageContext) {
String username = RestApiCommonUtil.getLoggedInUsername();
Subscriber subscriber = new Subscriber(username);
Set<SubscribedAPI> subscriptions;
List<SubscribedAPI> subscribedAPIList = new ArrayList<>();
// pre-processing
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
// currently groupId is taken from the user so that groupId coming as a query parameter is not honored.
// As a improvement, we can check admin privileges of the user and honor groupId.
groupId = RestApiUtil.getLoggedInUserGroupId();
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
SubscriptionListDTO subscriptionListDTO;
if (!StringUtils.isEmpty(apiId)) {
// todo : FIX properly, need to done properly with backend side pagination.
// todo : getSubscribedIdentifiers() method should NOT be used. Appears to be too slow.
// This will fail with an authorization failed exception if user does not have permission to access the API
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
if (apiTypeWrapper.isAPIProduct()) {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApiProduct().getId(), groupId, organization);
} else {
subscriptions = apiConsumer.getSubscribedIdentifiers(subscriber, apiTypeWrapper.getApi().getId(), groupId, organization);
}
// sort by application name
subscribedAPIList.addAll(subscriptions);
subscribedAPIList.sort(Comparator.comparing(o -> o.getApplication().getName()));
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
SubscriptionMappingUtil.setPaginationParams(subscriptionListDTO, apiId, "", limit, offset, subscribedAPIList.size());
return Response.ok().entity(subscriptionListDTO).build();
} else if (!StringUtils.isEmpty(applicationId)) {
Application application = apiConsumer.getApplicationByUUID(applicationId);
if (application == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
return null;
}
if (!RestAPIStoreUtils.isUserAccessAllowedForApplication(application)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_APPLICATION, applicationId, log);
}
subscriptions = apiConsumer.getPaginatedSubscribedAPIsByApplication(application, offset, limit, organization);
subscribedAPIList.addAll(subscriptions);
subscriptionListDTO = SubscriptionMappingUtil.fromSubscriptionListToDTO(subscribedAPIList, limit, offset, organization);
return Response.ok().entity(subscriptionListDTO).build();
} else {
// neither apiId nor applicationId is given
RestApiUtil.handleBadRequest("Either applicationId or apiId should be available", log);
return null;
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while getting subscriptions of the user " + username, e, log);
}
}
return null;
}
use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.
the class AuthorizationUtilsTest method getAuthorizationParts.
@Test
public void getAuthorizationParts() {
String type = "type";
String credentials = "credentials";
Message m = new MessageImpl();
m.put(Message.PROTOCOL_HEADERS, Collections.singletonMap(HttpHeaders.AUTHORIZATION, Collections.singletonList(type + ' ' + credentials)));
MessageContext mc = new MessageContextImpl(m);
String[] expected = new String[] { type, credentials };
assertArrayEquals(expected, AuthorizationUtils.getAuthorizationParts(mc, null));
assertArrayEquals(expected, AuthorizationUtils.getAuthorizationParts(mc, Collections.emptySet()));
assertArrayEquals(expected, AuthorizationUtils.getAuthorizationParts(mc, Collections.singleton(type)));
assertArrayEquals(expected, AuthorizationUtils.getAuthorizationParts(mc, Collections.singleton("*")));
assertArrayEquals(expected, AuthorizationUtils.getAuthorizationParts(mc, new HashSet<>(Arrays.asList("another", type))));
}
use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.
the class JAXBElementProvider method getStreamWriter.
protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
XMLStreamWriter writer = null;
MessageContext mc = getContext();
if (mc != null) {
writer = mc.getContent(XMLStreamWriter.class);
if (writer == null) {
XMLOutputFactory factory = (XMLOutputFactory) mc.get(XMLOutputFactory.class.getName());
if (factory != null) {
try {
writer = factory.createXMLStreamWriter(os);
} catch (XMLStreamException e) {
throw ExceptionUtils.toInternalServerErrorException(new RuntimeException("Cant' create XMLStreamWriter", e), null);
}
}
}
if (writer == null && getEnableStreaming()) {
writer = StaxUtils.createXMLStreamWriter(os);
}
}
if (writer == null && os == null) {
writer = getStreamHandlerFromCurrentMessage(XMLStreamWriter.class);
}
return createTransformWriterIfNeeded(writer, os, true);
}
use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.
the class JAXBElementProvider method getStreamReader.
protected XMLStreamReader getStreamReader(InputStream is, Class<?> type, MediaType mt) {
MessageContext mc = getContext();
XMLStreamReader reader = mc != null ? mc.getContent(XMLStreamReader.class) : null;
if (reader == null && mc != null) {
XMLInputFactory factory = (XMLInputFactory) mc.get(XMLInputFactory.class.getName());
if (factory != null) {
try {
reader = factory.createXMLStreamReader(is);
} catch (XMLStreamException e) {
throw ExceptionUtils.toInternalServerErrorException(new RuntimeException("Can not create XMLStreamReader", e), null);
}
}
}
if (reader == null && is == null) {
reader = getStreamHandlerFromCurrentMessage(XMLStreamReader.class);
}
reader = createTransformReaderIfNeeded(reader, is);
reader = createDepthReaderIfNeeded(reader, is);
if (InjectionUtils.isSupportedCollectionOrArray(type)) {
return new JAXBCollectionWrapperReader(TransformUtils.createNewReaderIfNeeded(reader, is));
}
return reader;
}
Aggregations