Search in sources :

Example 1 with UriBuilderException

use of javax.ws.rs.core.UriBuilderException in project ddf by codice.

the class KmlEndpoint method getAvailableSources.

/**
     * Creates a list of {@link NetworkLink}s, one for each {@link ddf.catalog.source.Source} including the local
     * catalog.
     *
     * @param uriInfo - injected resource provding the URI.
     * @return - {@link Kml} containing a folder of {@link NetworkLink}s.
     */
@GET
@Path(FORWARD_SLASH + "sources")
@Produces(KML_MIME_TYPE)
public Kml getAvailableSources(@Context UriInfo uriInfo) {
    try {
        SourceInfoResponse response = framework.getSourceInfo(new SourceInfoRequestEnterprise(false));
        Kml kml = KmlFactory.createKml();
        Folder folder = kml.createAndSetFolder();
        folder.setOpen(true);
        for (SourceDescriptor descriptor : response.getSourceInfo()) {
            UriBuilder builder = UriBuilder.fromUri(uriInfo.getBaseUri());
            builder = generateEndpointUrl(SystemBaseUrl.getRootContext() + FORWARD_SLASH + CATALOG_URL_PATH + FORWARD_SLASH + OPENSEARCH_URL_PATH, builder);
            builder = builder.queryParam(SOURCE_PARAM, descriptor.getSourceId());
            builder = builder.queryParam(OPENSEARCH_SORT_KEY, OPENSEARCH_DEFAULT_SORT);
            builder = builder.queryParam(OPENSEARCH_FORMAT_KEY, KML_TRANSFORM_PARAM);
            NetworkLink networkLink = generateViewBasedNetworkLink(builder.build().toURL(), descriptor.getSourceId());
            folder.getFeature().add(networkLink);
        }
        return kml;
    } catch (SourceUnavailableException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    } catch (UnknownHostException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    } catch (MalformedURLException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    } catch (IllegalArgumentException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    } catch (UriBuilderException e) {
        throw new WebApplicationException(e, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) SourceDescriptor(ddf.catalog.source.SourceDescriptor) MalformedURLException(java.net.MalformedURLException) WebApplicationException(javax.ws.rs.WebApplicationException) UnknownHostException(java.net.UnknownHostException) Kml(de.micromata.opengis.kml.v_2_2_0.Kml) UriBuilderException(javax.ws.rs.core.UriBuilderException) Folder(de.micromata.opengis.kml.v_2_2_0.Folder) SourceInfoRequestEnterprise(ddf.catalog.operation.impl.SourceInfoRequestEnterprise) NetworkLink(de.micromata.opengis.kml.v_2_2_0.NetworkLink) UriBuilder(javax.ws.rs.core.UriBuilder) SourceInfoResponse(ddf.catalog.operation.SourceInfoResponse) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with UriBuilderException

use of javax.ws.rs.core.UriBuilderException in project che by eclipse.

the class RecipeDownloader method getRecipe.

/**
     * Downloads recipe by location.
     *
     * @param location
     *         location of recipe
     * @return recipe with set content and type
     * @throws ServerException
     *         if any error occurs
     */
public String getRecipe(String location) throws ServerException {
    URL recipeUrl;
    File file = null;
    try {
        UriBuilder targetUriBuilder = UriBuilder.fromUri(location);
        // add user token to be able to download user's private recipe
        final URI recipeUri = targetUriBuilder.build();
        if (!recipeUri.isAbsolute() && recipeUri.getHost() == null) {
            targetUriBuilder.scheme(apiEndpoint.getScheme()).host(apiEndpoint.getHost()).port(apiEndpoint.getPort()).replacePath(apiEndpoint.getPath() + location);
            if (EnvironmentContext.getCurrent().getSubject().getToken() != null) {
                targetUriBuilder.queryParam("token", EnvironmentContext.getCurrent().getSubject().getToken());
            }
        }
        recipeUrl = targetUriBuilder.build().toURL();
        file = IoUtil.downloadFileWithRedirect(null, "recipe", null, recipeUrl);
        return IoUtil.readAndCloseQuietly(new FileInputStream(file));
    } catch (IOException | IllegalArgumentException | UriBuilderException e) {
        throw new MachineException(format("Failed to download recipe %s. Error: %s", location, e.getLocalizedMessage()));
    } finally {
        if (file != null && !file.delete()) {
            LOG.error(String.format("Removal of recipe file %s failed.", file.getAbsolutePath()));
        }
    }
}
Also used : MachineException(org.eclipse.che.api.machine.server.exception.MachineException) IOException(java.io.IOException) UriBuilderException(javax.ws.rs.core.UriBuilderException) UriBuilder(javax.ws.rs.core.UriBuilder) File(java.io.File) URI(java.net.URI) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Example 3 with UriBuilderException

use of javax.ws.rs.core.UriBuilderException in project jersey by jersey.

the class ServletContainer method service.

/**
     * Receives standard HTTP requests from the public {@code service} method and dispatches
     * them to the {@code do}<i>XXX</i> methods defined in
     * this class. This method is an HTTP-specific version of the
     * {@link javax.servlet.Servlet#service} method. There's no
     * need to override this method.
     *
     * @param request  the {@link HttpServletRequest} object that
     *                 contains the request the client made of
     *                 the servlet
     * @param response the {@link HttpServletResponse} object that
     *                 contains the response the servlet returns
     *                 to the client
     * @throws IOException      if an input or output error occurs
     *                          while the servlet is handling the
     *                          HTTP request
     * @throws ServletException if the HTTP request
     *                          cannot be handled
     * @see javax.servlet.Servlet#service
     */
@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    /**
         * There is an annoying edge case where the service method is
         * invoked for the case when the URI is equal to the deployment URL
         * minus the '/', for example http://locahost:8080/HelloWorldWebApp
         */
    final String servletPath = request.getServletPath();
    final StringBuffer requestUrl = request.getRequestURL();
    final String requestURI = request.getRequestURI();
    //        final String pathInfo = request.getPathInfo();
    //        final boolean checkPathInfo = pathInfo == null || pathInfo.isEmpty() || pathInfo.equals("/");
    //        if (checkPathInfo && !request.getRequestURI().endsWith("/")) {
    // Only do this if the last segment of the servlet path does not contain '.'
    // This handles the case when the extension mapping is used with the servlet
    // see issue 506
    // This solution does not require parsing the deployment descriptor,
    // however still leaves it broken for the very rare case if a standard path
    // servlet mapping would include dot in the last segment (e.g. /.webresources/*)
    // and somebody would want to hit the root resource without the trailing slash
    //            final int i = servletPath.lastIndexOf('/');
    //            if (servletPath.substring(i + 1).indexOf('.') < 0) {
    // TODO (+ handle request URL with invalid characters - see the creation of absoluteUriBuilder below)
    //                if (webComponent.getResourceConfig().getFeature(ResourceConfig.FEATURE_REDIRECT)) {
    //                    URI l = UriBuilder.fromUri(request.getRequestURL().toString()).
    //                            path("/").
    //                            replaceQuery(request.getQueryString()).build();
    //
    //                    response.setStatus(307);
    //                    response.setHeader("Location", l.toASCIIString());
    //                    return;
    //                } else {
    //                pathInfo = "/";
    //                requestURL.append("/");
    //                requestURI += "/";
    //                }
    //            }
    //        }
    /**
         * The HttpServletRequest.getRequestURL() contains the complete URI
         * minus the query and fragment components.
         */
    final UriBuilder absoluteUriBuilder;
    try {
        absoluteUriBuilder = UriBuilder.fromUri(requestUrl.toString());
    } catch (final IllegalArgumentException iae) {
        setResponseForInvalidUri(response, iae);
        return;
    }
    /**
         * The HttpServletRequest.getPathInfo() and
         * HttpServletRequest.getServletPath() are in decoded form.
         *
         * On some servlet implementations the getPathInfo() removed
         * contiguous '/' characters. This is problematic if URIs
         * are embedded, for example as the last path segment.
         * We need to work around this and not use getPathInfo
         * for the decodedPath.
         */
    final String decodedBasePath = request.getContextPath() + servletPath + "/";
    final String encodedBasePath = UriComponent.encode(decodedBasePath, UriComponent.Type.PATH);
    if (!decodedBasePath.equals(encodedBasePath)) {
        throw new ProcessingException("The servlet context path and/or the " + "servlet path contain characters that are percent encoded");
    }
    final URI baseUri;
    final URI requestUri;
    try {
        baseUri = absoluteUriBuilder.replacePath(encodedBasePath).build();
        String queryParameters = ContainerUtils.encodeUnsafeCharacters(request.getQueryString());
        if (queryParameters == null) {
            queryParameters = "";
        }
        requestUri = absoluteUriBuilder.replacePath(requestURI).replaceQuery(queryParameters).build();
    } catch (final UriBuilderException | IllegalArgumentException ex) {
        setResponseForInvalidUri(response, ex);
        return;
    }
    service(baseUri, requestUri, request, response);
}
Also used : UriBuilderException(javax.ws.rs.core.UriBuilderException) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) ProcessingException(javax.ws.rs.ProcessingException)

Aggregations

UriBuilder (javax.ws.rs.core.UriBuilder)3 UriBuilderException (javax.ws.rs.core.UriBuilderException)3 URI (java.net.URI)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)1 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)1 SourceDescriptor (ddf.catalog.source.SourceDescriptor)1 SourceUnavailableException (ddf.catalog.source.SourceUnavailableException)1 Folder (de.micromata.opengis.kml.v_2_2_0.Folder)1 Kml (de.micromata.opengis.kml.v_2_2_0.Kml)1 NetworkLink (de.micromata.opengis.kml.v_2_2_0.NetworkLink)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 ProcessingException (javax.ws.rs.ProcessingException)1 Produces (javax.ws.rs.Produces)1