protected void addQueryStringPayload(HTTPRequest httpRequest, QueryStringPayload queryStringPayload) { Iterator params = queryStringPayload.iterator(); while (params.hasNext()) { Map.Entry param = (Map.Entry) params.next(); String val = (String) param.getValue(); String paramKey = ((Key) param.getKey()).getKey(); String paramValue = val == null ? "" : val; httpRequest.parameter(paramKey, paramValue); } LOG.debug("Adding query string -> {}", queryStringPayload.toString()); }
private void addGzipAsSupportedEncoding(HTTPRequest httpRequest) { httpRequest.header("accept-encoding", "gzip"); LOG.debug("Adding request header -> accept-encoding: gzip"); }
/** * Performs the service request "CloseSession". * * @throws ClientException if something went wrong during the request * @throws org.n52.security.common.artifact.ServiceException if the response is of MIME type * <code>application/vnd.ogc.se_xml */ public void closeSession(String version, String sessionID) throws ClientException, ServiceException { HTTPRequest httpRequest = httpClientFactory.create(getWssURLString()).get(); httpRequest.parameter("VERSION", version, "SESSIONID", sessionID, "SERVICE", "WSS", "REQUEST", "CloseSession"); HTTPResponse<InputStream> httpResponse = httpRequest.asStream().start(); if (httpResponse.isError()) { LOG.error("", httpResponse.getError()); throw new ClientException("I/O exception during CLOSESESSION request to Security Service:\n" + httpResponse.getStatus().getCode() + ": " + ((httpResponse.getError() != null) ? httpResponse.getError().getMessage() : " no exception available")); } }
private String getSSOSessionId(String username, char[] password) throws FailedLoginException { HTTPClient client = m_httpClientFactory.create(m_endpoint); HTTPRequest req; switch (m_authnMode) { case AUTHN_MODE_USER_PW: req = client.post().parameter(m_usernameParam, username, m_passwordParam, new String(password)); break; default: req = client.get().basicAuth(username, password); break; } HTTPResponse<String> response = req.asString().start(); if (response.isError()) { if (response.getStatus() == HTTPCode.FORBIDDEN) { throw new FailedLoginException("Wrong credentials"); } LOG.error("Remote service connection failed: " + req.getEndpoint(), response.getError()); throw new FailedLoginException("Remote service connection failed: " + response.getError()); } return response.getContent().trim(); }
HTTPResponse<InputStream> httpResponse = httpRequest.as(InputStream.class).start(); Transferable lRes = handleResponse(httpResponse, requestTransferable);
/** * Responsible for adding http username and password to the http client as http basic authentication. * * @param httpRequest the http request. * @param pRequest the current request. */ protected void addHttpAuthentication(final HTTPRequest httpRequest, final Transferable pRequest) { TransferAttribute userattr = pRequest.getAttribute(TransferAttribute.HTTP_AUTH_BASIC_USERNAME); TransferAttribute pw = pRequest.getAttribute(TransferAttribute.HTTP_AUTH_BASIC_PASSWORD); if (userattr != null && userattr.getValue() != null) { String clearpw = pw != null ? (String) pw.getValue() : null; if (clearpw != null) { // password is hold in memory as base64 clearpw = StringUtils.decodeBase64(clearpw); } httpRequest.basicAuth((String) userattr.getValue(), clearpw.toCharArray()); } }
public SessionInfo getSession(String version, Credential credential) throws ClientException, ServiceException { String authMethodUrn = guessAuthenticationMethodUrn(credential); // FIXME: if type of the credential is not given, the was urn is used. String authnMethod = authMethodUrn == null ? WASAuthenticationMethod.URN : authMethodUrn; String encodedCredential = getCredentialFactory().encode(authnMethod, credential); HTTPRequest httpRequest = httpClientFactory.create(getWssURLString()).post() .contentType("application/x-www-form-urlencoded", "UTF-8") .parameter("VERSION", version, "CREDENTIALS", encodedCredential, "SERVICE", "WSS", "REQUEST", "GetSession", "AUTHMETHOD", "authnMethod"); HTTPResponse<InputStream> httpResponse = httpRequest.asStream().start(); if (httpResponse.isError()) { LOG.error(httpResponse.getError().getMessage(), httpResponse.getError()); throw new ClientException("I/O exception during GETSESSION request to Security Service:\n" + httpResponse.getError(), ClientException.CONNECTION_FAILED); } String contentType = httpResponse.getContentType(); if (contentType.startsWith("text/xml")) { return SessionInfo.createFromXML(httpResponse.getContent()); } else if (contentType.startsWith("application/vnd.ogc.se_xml")) { throw ServiceException.createFromXML(new InputSource(httpResponse.getContent())); } else { LOG.error("GetSession response contained no session information. Response is: " + StringUtils.streamToString(httpResponse.getContent(), "UTF-8")); throw new ClientException("Security Service response specifies unknown/unexpected MIME type: " + contentType + ". Session information unavailable.", ClientException.UNEXPECTED_MIME_TYPE); } }
public WSSCapabilitiesDocument getCapabilities(String version, String service) throws ClientException, ServiceException { HTTPRequest httpRequest = httpClientFactory.create(getWssURLString()).get(); httpRequest.parameter("SERVICE", service, "REQUEST", "GetCapabilities"); try { HTTPResponse<InputStream> httpResponse = httpRequest.asStream().start(); if (!httpResponse.getStatus().is(200)) { throw new ClientException("Error during WSS GetCapabilities request: " + httpResponse.getStatus().toString()); } String contentType = httpResponse.getContentType(); if (contentType.startsWith("text/xml")) { return WSSCapabilitiesDocument.parse(httpResponse.getContent()); } else if (contentType.startsWith("application/vnd.ogc.se_xml")) { throw ServiceException.createFromXML(new InputSource(httpResponse.getContent())); } else { throw new ClientException("WSS GetCapabilities response must be of MIME type text/xml. Found: " + contentType + ". Capabilities information unavailable.", ClientException.UNEXPECTED_MIME_TYPE); } } catch (Exception e) { LOG.error(e.getMessage(), e); throw new ClientException("Error while processing GETCAPABILITIES response from Security Service:\n" + e.getMessage(), ClientException.CONNECTION_FAILED); } }
private <T> T get(String sessionID, String action, Class<T> expectedResponse) { HTTPClient client = m_clientFactory.create(m_sessionServiceUrl); HTTPRequest request; if (action != null) { request = client.get(sessionID, action); } else { request = client.get(sessionID); } HTTPResponse<T> response = request.as(expectedResponse).start(); if (response.isError()) { throw new InvalidSessionIDException(sessionID, response.getError()); } return response.getContent(); }
public Document build() throws WebServiceDescriptionException { requestParameters = requestParameters == null ? new String[0] : requestParameters; HTTPRequest httpRequest = httpClientFactory.create(url).get(); HTTPResponse<InputStream> responseStream = httpRequest.parameter(requestParameters).asStream().start(); if (isInvalidResponse(responseStream)) { LOG.warn("Could not read capabilities from {}" + url); LOG.warn("Http response status code is {}", responseStream.getStatus().getCode()); throw new WebServiceDescriptionException("Could not read capabilities from " + url); } try { response = DOMUtil.createFromStream(responseStream.getContent(), DOMParserOptions.getDefault().expandEntityReferences()); } catch(Exception e) { throw new WebServiceDescriptionException("Could not parse capabilities document: " + e.getMessage()); }finally { requestParameters = null; // reset parameters } if (asIsoDocument) { return buildIsoDocument(); } return response; }
protected HTTPRequest processHttpAttributes(HTTPRequest httpRequest, Transferable requestTransferable) throws IOException { Map<String, String> httpHeaderAttributes = getHttpHeaderAttributes(requestTransferable); httpRequest.header(httpHeaderAttributes); QueryStringPayload queryStringPayload = getQueryStringPayload(requestTransferable); if (queryStringPayload != null) { addQueryStringPayload(httpRequest, queryStringPayload); } if (!httpHeaderAttributes.containsKey("accept-encoding")) { addGzipAsSupportedEncoding(httpRequest); } return httpRequest; }