Java Code Examples for org.apache.cxf.jaxrs.utils.ResourceUtils#getResourceStream()

The following examples show how to use org.apache.cxf.jaxrs.utils.ResourceUtils#getResourceStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SourceGenerator.java    From cxf with Apache License 2.0 6 votes vote down vote up
private Element readDocument(String href) {
    try {
        InputStream is = null;
        if (!href.startsWith("http")) {
            is = ResourceUtils.getResourceStream(href, bus);
        }
        if (is == null) {
            URL url = new URL(href);
            if (href.startsWith("https") && authentication != null) {
                is = SecureConnectionHelper.getStreamFromSecureConnection(url, authentication);
            } else {
                is = url.openStream();
            }
        }
        try (Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8)) {
            return StaxUtils.read(new InputSource(reader)).getDocumentElement();
        }
    } catch (Exception ex) {
        throw new RuntimeException("Resource " + href + " can not be read");
    }
}
 
Example 2
Source File: WadlGenerator.java    From cxf with Apache License 2.0 6 votes vote down vote up
private String transformLocally(Message m, UriInfo ui, Source source) throws Exception {
    InputStream is = ResourceUtils.getResourceStream(stylesheetReference, m.getExchange().getBus());
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    try {
        transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
    } catch (IllegalArgumentException ex) {
        // ignore
    }

    Transformer t = transformerFactory.newTemplates(new StreamSource(is)).newTransformer();
    t.setParameter("base.path", m.get("http.base.path"));
    StringWriter stringWriter = new StringWriter();
    t.transform(source, new StreamResult(stringWriter));
    return stringWriter.toString();
}
 
Example 3
Source File: WadlGenerator.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void loadSchemasIntoCache(String loc) throws Exception {
    InputStream is = ResourceUtils.getResourceStream(loc,
        bus == null ? BusFactory.getDefaultBus() : bus);
    if (is == null) {
        return;
    }
    try (ByteArrayInputStream bis = IOUtils.loadIntoBAIS(is)) {
        XMLSource source = new XMLSource(bis);
        source.setBuffering();
        String targetNs = source.getValue("/*/@targetNamespace");

        Map<String, String> nsMap = Collections.singletonMap("xs", Constants.URI_2001_SCHEMA_XSD);
        String[] elementNames = source.getValues("/*/xs:element/@name", nsMap);
        externalQnamesMap.put(targetNs, Arrays.asList(elementNames));
        String schemaValue = source.getNode("/xs:schema", nsMap, String.class);
        externalSchemasCache.add(schemaValue);
    }
}
 
Example 4
Source File: OpenApiParseUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static UserApplication getUserApplication(String loc, Bus bus, ParseConfiguration cfg) {    
    try {
        InputStream is = ResourceUtils.getResourceStream(loc, bus);
        if (is == null) {
            return null;
        }
        return getUserApplicationFromStream(is, cfg);
    } catch (Exception ex) {
        LOG.warning("Problem with processing a user model at " + loc);
    }
    return null;
}
 
Example 5
Source File: SwaggerToOpenApiConversionUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static String getOpenApiFromSwaggerLoc(String loc, OpenApiConfiguration cfg, Bus bus) {
    try {
        InputStream is = ResourceUtils.getResourceStream(loc, bus);
        if (is == null) {
            return null;
        }
        return getOpenApiFromSwaggerStream(is, cfg);
    } catch (Exception ex) {
        LOG.warning("Problem with processing a user model at " + loc + ", exception: "
            + ExceptionUtils.getStackTrace(ex));
    }
    return null;
}
 
Example 6
Source File: SwaggerParseUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static UserApplication getUserApplication(String loc, Bus bus, ParseConfiguration cfg) {    
    try {
        InputStream is = ResourceUtils.getResourceStream(loc, bus);
        if (is == null) {
            return null;
        }
        return getUserApplicationFromStream(is, cfg);
    } catch (Exception ex) {
        LOG.warning("Problem with processing a user model at " + loc);
    }
    return null;
}
 
Example 7
Source File: WadlGenerator.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Response getExistingWadl(Message m, UriInfo ui, MediaType mt) {
    Endpoint ep = m.getExchange().getEndpoint();
    if (ep != null) {
        String loc = (String)ep.get(JAXRSUtils.DOC_LOCATION);
        if (loc != null) {
            try {
                InputStream is = ResourceUtils.getResourceStream(loc, (Bus)ep.get(Bus.class.getName()));
                if (is != null) {
                    Object contextProp = m.getContextualProperty(CONVERT_WADL_RESOURCES_TO_DOM);
                    boolean doConvertResourcesToDOM = contextProp == null
                        ? convertResourcesToDOM : PropertyUtils.isTrue(contextProp);
                    if (!doConvertResourcesToDOM || isJson(mt)) {
                        return Response.ok(is, mt).build();
                    }
                    Document wadlDoc = StaxUtils.read(is);
                    Element appEl = wadlDoc.getDocumentElement();

                    List<Element> grammarEls = DOMUtils.getChildrenWithName(appEl, WadlGenerator.WADL_NS,
                                                                            "grammars");
                    if (grammarEls.size() == 1) {
                        handleExistingDocRefs(DOMUtils.getChildrenWithName(grammarEls.get(0),
                                                                           WadlGenerator.WADL_NS,
                                                                           "include"), "href", loc, "",
                                              m, ui);
                    }

                    List<Element> resourcesEls = DOMUtils.getChildrenWithName(appEl,
                                                                              WadlGenerator.WADL_NS,
                                                                              "resources");
                    if (resourcesEls.size() == 1) {
                        DOMUtils.setAttribute(resourcesEls.get(0), "base", getBaseURI(m, ui));

                        List<Element> resourceEls = DOMUtils.getChildrenWithName(resourcesEls.get(0),
                                                                                 WadlGenerator.WADL_NS,
                                                                                 "resource");
                        handleExistingDocRefs(resourceEls, "type", loc, "", m, ui);
                        return finalizeExistingWadlResponse(wadlDoc, m, ui, mt);
                    }

                }
            } catch (Exception ex) {
                throw ExceptionUtils.toInternalServerErrorException(ex, null);
            }
        }
    }
    return null;
}