Search in sources :

Example 1 with ResourceRetrievalException

use of com.predic8.membrane.core.resolver.ResourceRetrievalException in project service-proxy by membrane.

the class WSDLPublisherInterceptor method handleRequest.

@Override
public Outcome handleRequest(final Exchange exc) throws Exception {
    if (!"GET".equals(exc.getRequest().getMethod()))
        return Outcome.CONTINUE;
    try {
        String resource = null;
        if (exc.getRequestURI().endsWith("?wsdl") || exc.getRequestURI().endsWith("?WSDL")) {
            processDocuments(exc);
            exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = wsdl));
            exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
        }
        if (exc.getRequestURI().contains("?xsd=")) {
            Map<String, String> params = URLParamUtil.getParams(router.getUriFactory(), exc);
            if (params.containsKey("xsd")) {
                int n = Integer.parseInt(params.get("xsd"));
                String path;
                processDocuments(exc);
                synchronized (paths) {
                    if (!paths.containsKey(n)) {
                        exc.setResponse(Response.forbidden("Unknown parameter. You may only retrieve documents referenced by the WSDL.").build());
                        return Outcome.ABORT;
                    }
                    path = paths.get(n);
                }
                exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), resource = path));
                exc.getResponse().getHeader().setContentType(MimeType.TEXT_XML);
            }
        }
        if (resource != null) {
            WSDLInterceptor wi = new WSDLInterceptor();
            wi.setRewriteEndpoint(false);
            wi.setPathRewriter(new RelativePathRewriter(exc, resource));
            wi.handleResponse(exc);
            return Outcome.RETURN;
        }
    } catch (NumberFormatException e) {
        exc.setResponse(HttpUtil.setHTMLErrorResponse(Response.internalServerError(), "Bad parameter format.", ""));
        return Outcome.ABORT;
    } catch (ResourceRetrievalException e) {
        exc.setResponse(Response.notFound().build());
        return Outcome.ABORT;
    }
    return Outcome.CONTINUE;
}
Also used : WSDLInterceptor(com.predic8.membrane.core.interceptor.WSDLInterceptor) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException)

Example 2 with ResourceRetrievalException

use of com.predic8.membrane.core.resolver.ResourceRetrievalException in project service-proxy by membrane.

the class FileSchemaWebAppResolver method resolve.

@Override
public InputStream resolve(String url) throws ResourceRetrievalException {
    if (url.startsWith("file:"))
        url = url.substring(5);
    log.debug("loading resource from: " + url);
    InputStream is = ctx.getResourceAsStream(url);
    if (is == null)
        throw new ResourceRetrievalException(url);
    return is;
}
Also used : InputStream(java.io.InputStream) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException)

Example 3 with ResourceRetrievalException

use of com.predic8.membrane.core.resolver.ResourceRetrievalException in project service-proxy by membrane.

the class ApiManagementConfiguration method updateAfterLocationChange.

public void updateAfterLocationChange() throws IOException {
    if (!isLocalFile(location)) {
        log.info("Loading configuration from [" + location + "]");
        if (location.startsWith("etcd")) {
            handleEtcd(location);
        } else {
            try {
                parseAndConstructConfiguration(getResolver().resolve(location));
            } catch (ResourceRetrievalException e) {
                log.error("Could not retrieve resource");
                return;
            }
        }
        return;
    } else {
        if (location.isEmpty())
            location = "api.yaml";
        final String newLocation = ResolverMap.combine(getCurrentDir(), location);
        log.info("Loading configuration from [" + newLocation + "]");
        InputStream is = null;
        try {
            is = getResolver().resolve(newLocation);
        } catch (ResourceRetrievalException e) {
            log.error("Could not retrieve resource");
            return;
        }
        parseAndConstructConfiguration(is);
        try {
            getResolver().observeChange(newLocation, new Consumer<InputStream>() {

                @Override
                public void call(InputStream inputStream) {
                    log.info("Loading configuration from [" + newLocation + "]");
                    if (!getContextLost()) {
                        try {
                            parseAndConstructConfiguration(inputStream);
                            getResolver().observeChange(newLocation, this);
                        } catch (ResourceRetrievalException ignored) {
                        } catch (IOException ignored) {
                        }
                    }
                }
            });
        } catch (Exception warn) {
            URL url = null;
            try {
                url = new URL(newLocation);
            } catch (MalformedURLException ignored) {
            }
            String schema = "";
            if (url != null) {
                schema = url.getProtocol();
            }
            log.warn("Could not observe AMC location for " + schema);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) IOException(java.io.IOException) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException) URISyntaxException(java.net.URISyntaxException) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) URL(java.net.URL)

Example 4 with ResourceRetrievalException

use of com.predic8.membrane.core.resolver.ResourceRetrievalException in project service-proxy by membrane.

the class HTTPSchemaResolver method resolve.

public InputStream resolve(String url) throws ResourceRetrievalException {
    try {
        Exchange exc = new Request.Builder().method(Request.METHOD_GET).url(uriFactory, url).header(Header.USER_AGENT, Constants.PRODUCT_NAME + " " + Constants.VERSION).buildExchange();
        Response response = getHttpClient().call(exc).getResponse();
        response.readBody();
        if (response.getStatusCode() != 200) {
            ResourceRetrievalException rde = new ResourceRetrievalException(url, response.getStatusCode());
            throw rde;
        }
        return new ByteArrayInputStream(ByteUtil.getByteArrayData(response.getBodyAsStreamDecoded()));
    } catch (ResourceRetrievalException e) {
        throw e;
    } catch (Exception e) {
        ResourceRetrievalException rre = new ResourceRetrievalException(url, e);
        throw rre;
    }
}
Also used : Exchange(com.predic8.membrane.core.exchange.Exchange) Response(com.predic8.membrane.core.http.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 5 with ResourceRetrievalException

use of com.predic8.membrane.core.resolver.ResourceRetrievalException in project service-proxy by membrane.

the class WSDLPublisherInterceptor method processDocuments.

private void processDocuments(Exchange exc) throws Exception {
    // exc.response is set to garbage and should be discarded after this method
    synchronized (paths) {
        try {
            while (true) {
                String doc = documents_to_process.poll();
                if (doc == null)
                    break;
                log.debug("WSDLPublisherInterceptor: processing " + doc);
                exc.setResponse(WebServerInterceptor.createResponse(router.getResolverMap(), doc));
                WSDLInterceptor wi = new WSDLInterceptor();
                wi.setRewriteEndpoint(false);
                wi.setPathRewriter(new RelativePathRewriter(exc, doc));
                wi.handleResponse(exc);
            }
        } catch (ResourceRetrievalException e) {
            log.error("Could not recursively load all documents referenced by '" + wsdl + "'.", e);
        }
    }
}
Also used : WSDLInterceptor(com.predic8.membrane.core.interceptor.WSDLInterceptor) ResourceRetrievalException(com.predic8.membrane.core.resolver.ResourceRetrievalException)

Aggregations

ResourceRetrievalException (com.predic8.membrane.core.resolver.ResourceRetrievalException)6 WSDLInterceptor (com.predic8.membrane.core.interceptor.WSDLInterceptor)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 SSLParser (com.predic8.membrane.core.config.security.SSLParser)1 Exchange (com.predic8.membrane.core.exchange.Exchange)1 Response (com.predic8.membrane.core.http.Response)1 Definitions (com.predic8.wsdl.Definitions)1 Port (com.predic8.wsdl.Port)1 Service (com.predic8.wsdl.Service)1 WSDLParser (com.predic8.wsdl.WSDLParser)1 WSDLParserContext (com.predic8.wsdl.WSDLParserContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ConnectException (java.net.ConnectException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1