Search in sources :

Example 1 with MessageContext

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

the class XSLTJaxbProvider method createTemplates.

protected Templates createTemplates(Templates templates, Map<String, Object> configuredParams, Map<String, String> outProps) {
    if (templates == null) {
        if (supportJaxbOnly) {
            return null;
        }
        LOG.severe("No template is available");
        throw ExceptionUtils.toInternalServerErrorException(null, null);
    }
    TemplatesImpl templ = new TemplatesImpl(templates, uriResolver);
    MessageContext mc = getContext();
    if (mc != null) {
        UriInfo ui = mc.getUriInfo();
        MultivaluedMap<String, String> params = ui.getPathParameters();
        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
            String value = entry.getValue().get(0);
            int ind = value.indexOf(';');
            if (ind > 0) {
                value = value.substring(0, ind);
            }
            templ.setTransformerParameter(entry.getKey(), value);
        }
        List<PathSegment> segments = ui.getPathSegments();
        if (!segments.isEmpty()) {
            setTransformParameters(templ, segments.get(segments.size() - 1).getMatrixParameters());
        }
        setTransformParameters(templ, ui.getQueryParameters());
        templ.setTransformerParameter(ABSOLUTE_PATH_PARAMETER, ui.getAbsolutePath().toString());
        templ.setTransformerParameter(RELATIVE_PATH_PARAMETER, ui.getPath());
        templ.setTransformerParameter(BASE_PATH_PARAMETER, ui.getBaseUri().toString());
        if (configuredParams != null) {
            for (Map.Entry<String, Object> entry : configuredParams.entrySet()) {
                templ.setTransformerParameter(entry.getKey(), entry.getValue());
            }
        }
    }
    if (outProps != null) {
        templ.setOutProperties(outProps);
    }
    return templ;
}
Also used : PathSegment(javax.ws.rs.core.PathSegment) List(java.util.List) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) UriInfo(javax.ws.rs.core.UriInfo)

Example 2 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project tomee 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 3 with MessageContext

use of org.apache.cxf.jaxrs.ext.MessageContext in project tomee 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 4 with MessageContext

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

the class RetrieveApiArtifactsApiServiceImpl method retrieveApiArtifactsPost.

public Response retrieveApiArtifactsPost(String xWSO2Tenant, String gatewayLabel, String type, UUIDListDTO uuidList, MessageContext messageContext) throws APIManagementException {
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    RuntimeArtifactDto runtimeArtifactDto = APIArtifactGeneratorUtil.generateAPIArtifact(uuidList.getUuids(), "", "", gatewayLabel, type, xWSO2Tenant);
    if (runtimeArtifactDto != null) {
        if (runtimeArtifactDto.isFile()) {
            File artifact = (File) runtimeArtifactDto.getArtifact();
            StreamingOutput streamingOutput = (outputStream) -> {
                try {
                    Files.copy(artifact.toPath(), outputStream);
                } finally {
                    Files.delete(artifact.toPath());
                }
            };
            return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=apis.zip").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_ZIP).build();
        } else {
            SynapseArtifactListDTO synapseArtifactListDTO = new SynapseArtifactListDTO();
            if (runtimeArtifactDto.getArtifact() instanceof List) {
                synapseArtifactListDTO.setList((List<String>) runtimeArtifactDto.getArtifact());
                synapseArtifactListDTO.setCount(((List<String>) runtimeArtifactDto.getArtifact()).size());
            }
            return Response.ok().entity(synapseArtifactListDTO).header(RestApiConstants.HEADER_CONTENT_TYPE, RestApiConstants.APPLICATION_JSON).build();
        }
    } else {
        return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
    }
}
Also used : SynapseArtifactListDTO(org.wso2.carbon.apimgt.internal.service.dto.SynapseArtifactListDTO) Files(java.nio.file.Files) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) RuntimeArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.RuntimeArtifactGeneratorUtil) APIArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.APIArtifactGeneratorUtil) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) APIConstants(org.wso2.carbon.apimgt.impl.APIConstants) ArrayList(java.util.ArrayList) SubscriptionValidationDataUtil(org.wso2.carbon.apimgt.internal.service.utils.SubscriptionValidationDataUtil) List(java.util.List) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) Response(javax.ws.rs.core.Response) RestApiConstants(org.wso2.carbon.apimgt.rest.api.common.RestApiConstants) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) UUIDListDTO(org.wso2.carbon.apimgt.internal.service.dto.UUIDListDTO) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) RetrieveApiArtifactsApiService(org.wso2.carbon.apimgt.internal.service.RetrieveApiArtifactsApiService) SynapseArtifactListDTO(org.wso2.carbon.apimgt.internal.service.dto.SynapseArtifactListDTO) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) StreamingOutput(javax.ws.rs.core.StreamingOutput) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File)

Example 5 with MessageContext

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

the class RuntimeMetadataApiServiceImpl method runtimeMetadataGet.

public Response runtimeMetadataGet(String xWSO2Tenant, String apiId, String gatewayLabel, MessageContext messageContext) throws APIManagementException {
    xWSO2Tenant = SubscriptionValidationDataUtil.validateTenantDomain(xWSO2Tenant, messageContext);
    RuntimeArtifactDto runtimeArtifactDto = RuntimeArtifactGeneratorUtil.generateMetadataArtifact(xWSO2Tenant, apiId, gatewayLabel);
    if (runtimeArtifactDto != null) {
        File artifact = (File) runtimeArtifactDto.getArtifact();
        StreamingOutput streamingOutput = (outputStream) -> {
            try {
                Files.copy(artifact.toPath(), outputStream);
            } finally {
                Files.delete(artifact.toPath());
            }
        };
        return Response.ok(streamingOutput).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=deployment.json").header(RestApiConstants.HEADER_CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE).build();
    } else {
        return Response.status(Response.Status.NOT_FOUND).entity(RestApiUtil.getErrorDTO(ExceptionCodes.NO_API_ARTIFACT_FOUND)).build();
    }
}
Also used : Files(java.nio.file.Files) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) RuntimeArtifactGeneratorUtil(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.RuntimeArtifactGeneratorUtil) RestApiUtil(org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) APIConstants(org.wso2.carbon.apimgt.impl.APIConstants) SubscriptionValidationDataUtil(org.wso2.carbon.apimgt.internal.service.utils.SubscriptionValidationDataUtil) MessageContext(org.apache.cxf.jaxrs.ext.MessageContext) Response(javax.ws.rs.core.Response) RestApiConstants(org.wso2.carbon.apimgt.rest.api.common.RestApiConstants) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ExceptionCodes(org.wso2.carbon.apimgt.api.ExceptionCodes) RuntimeMetadataApiService(org.wso2.carbon.apimgt.internal.service.RuntimeMetadataApiService) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File)

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