Search in sources :

Example 6 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project carbon-apimgt by wso2.

the class AlertSubscriptionsApiServiceImpl method getSubscribedAlertTypes.

/**
 * Retrieves all the admin alert subscriptions of the logged in user
 *
 * @param messageContext
 * @return
 */
public Response getSubscribedAlertTypes(MessageContext messageContext) {
    String fullyQualifiedUsername = getFullyQualifiedUsername(RestApiCommonUtil.getLoggedInUsername());
    try {
        AdminAlertConfigurator adminAlertConfigurator = (AdminAlertConfigurator) AlertConfigManager.getInstance().getAlertConfigurator(AlertMgtConstants.ADMIN_DASHBOARD_AGENT);
        List<Integer> subscribedAlertTypes = adminAlertConfigurator.getSubscribedAlerts(fullyQualifiedUsername);
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> supportedAlertTypeDTOS = adminAlertConfigurator.getSupportedAlertTypes();
        // Filter out the subscribed alerts
        List<org.wso2.carbon.apimgt.impl.dto.AlertTypeDTO> subscribedAlertsList = supportedAlertTypeDTOS.stream().filter(supportedAlertTypes -> subscribedAlertTypes.stream().anyMatch(subscribedAlerts -> supportedAlertTypes.getId().equals(subscribedAlerts))).collect(Collectors.toList());
        // Retrieve the list of subscribed emails
        List<String> subscribedEmails = adminAlertConfigurator.getSubscribedEmailAddresses(fullyQualifiedUsername);
        AlertsSubscriptionDTO alertsSubscriptionDTO = new AlertsSubscriptionDTO();
        alertsSubscriptionDTO.setAlerts(AlertsMappingUtil.fromAlertTypesListToAlertTypeDTOList(subscribedAlertsList));
        alertsSubscriptionDTO.setEmailList(subscribedEmails);
        return Response.status(Response.Status.OK).entity(alertsSubscriptionDTO).build();
    } catch (AlertManagementException e) {
        return Response.status(Response.Status.NO_CONTENT).entity("API Manager analytics is not enabled").build();
    } catch (APIManagementException e) {
        RestApiUtil.handleInternalServerError("Internal Error occurred", e, log);
    }
    return Response.status(Response.Status.NO_CONTENT).build();
}
Also used : MultitenantConstants(org.wso2.carbon.utils.multitenancy.MultitenantConstants) APIAdminImpl(org.wso2.carbon.apimgt.impl.APIAdminImpl) AlertMgtConstants(org.wso2.carbon.apimgt.impl.alertmgt.AlertMgtConstants) APIAdmin(org.wso2.carbon.apimgt.api.APIAdmin) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) RestApiCommonUtil(org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil) AlertsMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.AlertsMappingUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) AlertConfigManager(org.wso2.carbon.apimgt.impl.alertmgt.AlertConfigManager) Map(java.util.Map) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) BotDetectionData(org.wso2.carbon.apimgt.api.model.botDataAPI.BotDetectionData) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) Collectors(java.util.stream.Collectors) AlertSubscriptionsApiService(org.wso2.carbon.apimgt.rest.api.admin.v1.AlertSubscriptionsApiService) BotDetectionAlertSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionDTO) List(java.util.List) BotDetectionAlertSubscriptionListDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.BotDetectionAlertSubscriptionListDTO) Response(javax.ws.rs.core.Response) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) BotDetectionMappingUtil(org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.BotDetectionMappingUtil) AlertsSubscriptionDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertsSubscriptionDTO) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AlertManagementException(org.wso2.carbon.apimgt.impl.alertmgt.exception.AlertManagementException) AlertTypeDTO(org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AlertTypeDTO) AdminAlertConfigurator(org.wso2.carbon.apimgt.impl.alertmgt.AdminAlertConfigurator)

Example 7 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.

the class JSONProvider method marshal.

protected void marshal(Marshaller ms, Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
    OutputStream actualOs = os;
    MessageContext mc = getContext();
    if (mc != null && PropertyUtils.isTrue(mc.get(Marshaller.JAXB_FORMATTED_OUTPUT))) {
        actualOs = new CachedOutputStream();
    }
    XMLStreamWriter writer = createWriter(actualObject, actualClass, genericType, enc, actualOs, isCollection);
    if (namespaceMap.size() > 1 || namespaceMap.size() == 1 && !namespaceMap.containsKey(JSONUtils.XSI_URI)) {
        setNamespaceMapper(ms, namespaceMap);
    }
    org.apache.cxf.common.jaxb.JAXBUtils.setNoEscapeHandler(ms);
    ms.marshal(actualObject, writer);
    writer.close();
    if (os != actualOs) {
        StringIndenter formatter = new StringIndenter(IOUtils.newStringFromBytes(((CachedOutputStream) actualOs).getBytes()));
        Writer outWriter = new OutputStreamWriter(os, enc);
        IOUtils.copy(new StringReader(formatter.result()), outWriter, 2048);
        outWriter.close();
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) OutputStream(java.io.OutputStream) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) StringIndenter(org.codehaus.jettison.util.StringIndenter) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) W3CDOMStreamWriter(org.apache.cxf.staxutils.W3CDOMStreamWriter) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 8 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.

the class SourceProvider method getPreferredSource.

protected String getPreferredSource() {
    MessageContext mc = getContext();
    String source = null;
    if (mc != null) {
        source = (String) mc.getContextualProperty(PREFERRED_FORMAT);
    }
    return source != null ? source : "sax";
}
Also used : MessageContext(org.apache.cxf.jaxrs.ext.MessageContext)

Example 9 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.

the class JAXBElementProvider method marshal.

// CHECKSTYLE:OFF
protected final void marshal(Object obj, Class<?> cls, Type genericType, String enc, OutputStream os, Annotation[] anns, MediaType mt, Marshaller ms) throws Exception {
    // CHECKSTYLE:ON
    for (Map.Entry<String, Object> entry : mProperties.entrySet()) {
        ms.setProperty(entry.getKey(), entry.getValue());
    }
    MessageContext mc = getContext();
    if (mc != null) {
        // they'll overwrite statically configured ones
        for (String key : MARSHALLER_PROPERTIES) {
            Object value = mc.get(key);
            if (value != null) {
                ms.setProperty(key, value);
            }
        }
    }
    XMLStreamWriter writer = getStreamWriter(obj, os, mt);
    if (writer != null) {
        if (os == null) {
            ms.setProperty(Marshaller.JAXB_FRAGMENT, true);
        } else if (mc != null) {
            if (mc.getContent(XMLStreamWriter.class) != null) {
                ms.setProperty(Marshaller.JAXB_FRAGMENT, true);
            }
            mc.put(XMLStreamWriter.class.getName(), writer);
        }
        marshalToWriter(ms, obj, writer, anns, mt);
        if (mc != null) {
            writer.writeEndDocument();
        }
    } else {
        marshalToOutputStream(ms, obj, os, anns, mt);
    }
}
Also used : XMLStreamWriter(javax.xml.stream.XMLStreamWriter) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Map(java.util.Map) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 10 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project cxf by apache.

the class JAXBElementProvider method resolveXMLResourceURI.

protected String resolveXMLResourceURI(String path) {
    MessageContext mc = getContext();
    if (mc != null) {
        String httpBasePath = (String) mc.get("http.base.path");
        final UriBuilder builder;
        if (httpBasePath != null) {
            builder = UriBuilder.fromPath(httpBasePath);
        } else {
            builder = mc.getUriInfo().getBaseUriBuilder();
        }
        return builder.path(path).path(xmlResourceOffset).build().toString();
    }
    return path;
}
Also used : MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) UriBuilder(javax.ws.rs.core.UriBuilder)

Aggregations

MessageContext (org.apache.cxf.jaxrs.ext.MessageContext)31 List (java.util.List)11 Map (java.util.Map)11 ArrayList (java.util.ArrayList)7 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)7 Response (javax.ws.rs.core.Response)7 MessageContextImpl (org.apache.cxf.jaxrs.ext.MessageContextImpl)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 RestApiUtil (org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil)7 HashMap (java.util.HashMap)6 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Message (org.apache.cxf.message.Message)5 ExceptionCodes (org.wso2.carbon.apimgt.api.ExceptionCodes)5 RestApiConstants (org.wso2.carbon.apimgt.rest.api.common.RestApiConstants)5 InputStream (java.io.InputStream)4 XMLStreamException (javax.xml.stream.XMLStreamException)4 URI (java.net.URI)3 MediaType (javax.ws.rs.core.MediaType)3 Log (org.apache.commons.logging.Log)3