Search in sources :

Example 41 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project knime-core by knime.

the class ProfileManager method downloadProfiles.

private Path downloadProfiles(final URI profileLocation) {
    Path stateDir = getStateLocation();
    Path profileDir = stateDir.resolve("profiles");
    try {
        Files.createDirectories(stateDir);
        URIBuilder builder = new URIBuilder(profileLocation);
        builder.addParameter("profiles", String.join(",", m_provider.getRequestedProfiles()));
        URI profileUri = builder.build();
        m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).info("Downloading profiles from " + profileUri));
        // proxies
        HttpHost proxy = ProxySelector.getDefault().select(profileUri).stream().filter(p -> p.address() != null).findFirst().map(p -> new HttpHost(((InetSocketAddress) p.address()).getAddress())).orElse(null);
        // timeout; we cannot access KNIMEConstants here because that would acccess preferences
        int timeout = 2000;
        String to = System.getProperty("knime.url.timeout", Integer.toString(timeout));
        try {
            timeout = Integer.parseInt(to);
        } catch (NumberFormatException ex) {
            m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).warn("Illegal value for system property knime.url.timeout :" + to, ex));
        }
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setProxy(proxy).setConnectionRequestTimeout(timeout).build();
        try (CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).setRedirectStrategy(new DefaultRedirectStrategy()).build()) {
            HttpGet get = new HttpGet(profileUri);
            if (Files.isDirectory(profileDir)) {
                Instant lastModified = Files.getLastModifiedTime(profileDir).toInstant();
                get.setHeader("If-Modified-Since", DateTimeFormatter.RFC_1123_DATE_TIME.format(lastModified.atZone(ZoneId.of("GMT"))));
            }
            try (CloseableHttpResponse response = client.execute(get)) {
                int code = response.getStatusLine().getStatusCode();
                if ((code >= 200) && (code < 300)) {
                    Header ct = response.getFirstHeader("Content-Type");
                    if ((ct == null) || (ct.getValue() == null) || !ct.getValue().startsWith("application/zip")) {
                        // no zip file - it just processes an empty zip
                        throw new IOException("Server did not return a ZIP file containing the selected profiles");
                    }
                    Path tempDir = PathUtils.createTempDir("profile-download", stateDir);
                    try (InputStream is = response.getEntity().getContent()) {
                        PathUtils.unzip(new ZipInputStream(is), tempDir);
                    }
                    // replace profiles only if new data has been downloaded successfully
                    PathUtils.deleteDirectoryIfExists(profileDir);
                    Files.move(tempDir, profileDir, StandardCopyOption.ATOMIC_MOVE);
                } else if (code != 304) {
                    // 304 = Not Modified
                    HttpEntity body = response.getEntity();
                    String msg;
                    if (body.getContentType().getValue().startsWith("text/")) {
                        byte[] buf = new byte[Math.min(4096, Math.max(4096, (int) body.getContentLength()))];
                        int read = body.getContent().read(buf);
                        msg = new String(buf, 0, read, "US-ASCII").trim();
                    } else if (!response.getStatusLine().getReasonPhrase().isEmpty()) {
                        msg = response.getStatusLine().getReasonPhrase();
                    } else {
                        msg = "Server returned status " + response.getStatusLine().getStatusCode();
                    }
                    throw new IOException(msg);
                }
            }
        }
    } catch (IOException | URISyntaxException ex) {
        String msg = "Could not download profiles from " + profileLocation + ": " + ex.getMessage() + ". " + (Files.isDirectory(profileDir) ? "Will use existing but potentially outdated profiles." : "No profiles will be applied.");
        m_collectedLogs.add(() -> NodeLogger.getLogger(ProfileManager.class).error(msg, ex));
    }
    return profileDir;
}
Also used : Path(java.nio.file.Path) Arrays(java.util.Arrays) ZipInputStream(java.util.zip.ZipInputStream) URISyntaxException(java.net.URISyntaxException) CoreException(org.eclipse.core.runtime.CoreException) RequestConfig(org.apache.http.client.config.RequestConfig) Supplier(java.util.function.Supplier) Header(org.apache.http.Header) StandardCopyOption(java.nio.file.StandardCopyOption) ArrayList(java.util.ArrayList) ProxySelector(java.net.ProxySelector) HashSet(java.util.HashSet) Charset(java.nio.charset.Charset) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IExtensionRegistry(org.eclipse.core.runtime.IExtensionRegistry) NodeLogger(org.knime.core.node.NodeLogger) IConfigurationElement(org.eclipse.core.runtime.IConfigurationElement) PathUtils(org.knime.core.util.PathUtils) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) URI(java.net.URI) Bundle(org.osgi.framework.Bundle) Path(java.nio.file.Path) OutputStream(java.io.OutputStream) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Properties(java.util.Properties) Files(java.nio.file.Files) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpEntity(org.apache.http.HttpEntity) IOException(java.io.IOException) Reader(java.io.Reader) Instant(java.time.Instant) InetSocketAddress(java.net.InetSocketAddress) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) HttpGet(org.apache.http.client.methods.HttpGet) DateTimeFormatter(java.time.format.DateTimeFormatter) DefaultPreferences(org.eclipse.core.internal.preferences.DefaultPreferences) Optional(java.util.Optional) Platform(org.eclipse.core.runtime.Platform) Collections(java.util.Collections) HttpHost(org.apache.http.HttpHost) FrameworkUtil(org.osgi.framework.FrameworkUtil) HttpClients(org.apache.http.impl.client.HttpClients) InputStream(java.io.InputStream) RequestConfig(org.apache.http.client.config.RequestConfig) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) Instant(java.time.Instant) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IExtensionPoint(org.eclipse.core.runtime.IExtensionPoint) URIBuilder(org.apache.http.client.utils.URIBuilder) ZipInputStream(java.util.zip.ZipInputStream) Header(org.apache.http.Header) HttpHost(org.apache.http.HttpHost) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) DefaultRedirectStrategy(org.apache.http.impl.client.DefaultRedirectStrategy)

Example 42 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project cas by apereo.

the class HttpUtils method buildHttpUri.

private static URI buildHttpUri(final String url, final Map<String, String> parameters) throws URISyntaxException {
    final URIBuilder uriBuilder = new URIBuilder(url);
    parameters.forEach(uriBuilder::addParameter);
    return uriBuilder.build();
}
Also used : URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 43 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project cas by apereo.

the class RemoteEndpointServiceAccessStrategy method doPrincipalAttributesAllowServiceAccess.

@Override
public boolean doPrincipalAttributesAllowServiceAccess(final String principal, final Map<String, Object> principalAttributes) {
    try {
        if (super.doPrincipalAttributesAllowServiceAccess(principal, principalAttributes)) {
            final HttpClient client = ApplicationContextProvider.getApplicationContext().getBean("noRedirectHttpClient", HttpClient.class);
            final URIBuilder builder = new URIBuilder(this.endpointUrl);
            builder.addParameter("username", principal);
            final URL url = builder.build().toURL();
            final HttpMessage message = client.sendMessageToEndPoint(url);
            LOGGER.debug("Message received from [{}] is [{}]", url, message);
            return message != null && StringUtils.commaDelimitedListToSet(this.acceptableResponseCodes).contains(String.valueOf(message.getResponseCode()));
        }
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return false;
}
Also used : HttpClient(org.apereo.cas.util.http.HttpClient) HttpMessage(org.apereo.cas.util.http.HttpMessage) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 44 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project cas by apereo.

the class BaseSamlRegisteredServiceAttributeReleasePolicy method getAttributesInternal.

@Override
public Map<String, Object> getAttributesInternal(final Principal principal, final Map<String, Object> attributes, final RegisteredService service) {
    if (service instanceof SamlRegisteredService) {
        final SamlRegisteredService saml = (SamlRegisteredService) service;
        final HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
        if (request == null) {
            LOGGER.warn("Could not locate the request context to process attributes");
            return super.getAttributesInternal(principal, attributes, service);
        }
        String entityId = request.getParameter(SamlProtocolConstants.PARAMETER_ENTITY_ID);
        if (StringUtils.isBlank(entityId)) {
            final String svcParam = request.getParameter(CasProtocolConstants.PARAMETER_SERVICE);
            if (StringUtils.isNotBlank(svcParam)) {
                try {
                    final URIBuilder builder = new URIBuilder(svcParam);
                    entityId = builder.getQueryParams().stream().filter(p -> p.getName().equals(SamlProtocolConstants.PARAMETER_ENTITY_ID)).map(NameValuePair::getValue).findFirst().orElse(StringUtils.EMPTY);
                } catch (final Exception e) {
                    LOGGER.error(e.getMessage());
                }
            }
        }
        final ApplicationContext ctx = ApplicationContextProvider.getApplicationContext();
        if (ctx == null) {
            LOGGER.warn("Could not locate the application context to process attributes");
            return super.getAttributesInternal(principal, attributes, service);
        }
        final SamlRegisteredServiceCachingMetadataResolver resolver = ctx.getBean("defaultSamlRegisteredServiceCachingMetadataResolver", SamlRegisteredServiceCachingMetadataResolver.class);
        final Optional<SamlRegisteredServiceServiceProviderMetadataFacade> facade = SamlRegisteredServiceServiceProviderMetadataFacade.get(resolver, saml, entityId);
        if (facade == null || !facade.isPresent()) {
            LOGGER.warn("Could not locate metadata for [{}] to process attributes", entityId);
            return super.getAttributesInternal(principal, attributes, service);
        }
        final EntityDescriptor input = facade.get().getEntityDescriptor();
        if (input == null) {
            LOGGER.warn("Could not locate entity descriptor for [{}] to process attributes", entityId);
            return super.getAttributesInternal(principal, attributes, service);
        }
        return getAttributesForSamlRegisteredService(attributes, saml, ctx, resolver, facade.get(), input);
    }
    return super.getAttributesInternal(principal, attributes, service);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EntityDescriptor(org.opensaml.saml.saml2.metadata.EntityDescriptor) ApplicationContext(org.springframework.context.ApplicationContext) SamlRegisteredServiceServiceProviderMetadataFacade(org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade) ServletRequestAttributes(org.springframework.web.context.request.ServletRequestAttributes) SamlRegisteredServiceCachingMetadataResolver(org.apereo.cas.support.saml.services.idp.metadata.cache.SamlRegisteredServiceCachingMetadataResolver) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 45 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project gocd by gocd.

the class MingleConfig method urlFor.

public String urlFor(String path) throws MalformedURLException, URISyntaxException {
    URIBuilder baseUri = new URIBuilder(baseUrl);
    String originalPath = baseUri.getPath();
    if (originalPath == null) {
        originalPath = "";
    }
    if (originalPath.endsWith(DELIMITER) && path.startsWith(DELIMITER)) {
        path = path.replaceFirst(DELIMITER, "");
    }
    return baseUri.setPath(originalPath + path).toString();
}
Also used : URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

URIBuilder (org.apache.http.client.utils.URIBuilder)107 URISyntaxException (java.net.URISyntaxException)42 URI (java.net.URI)37 HttpGet (org.apache.http.client.methods.HttpGet)22 IOException (java.io.IOException)21 NameValuePair (org.apache.http.NameValuePair)13 HttpEntity (org.apache.http.HttpEntity)10 NotNull (org.jetbrains.annotations.NotNull)9 Map (java.util.Map)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 HttpResponse (org.apache.http.HttpResponse)7 List (java.util.List)6 HttpClient (org.apache.http.client.HttpClient)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 Gson (com.google.gson.Gson)4 URL (java.net.URL)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4