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;
}
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();
}
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;
}
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);
}
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();
}
Aggregations