/** * Creates a licene reference from an XML string. It is a shortcut for {@link #createFrom(java.io.Reader)} called with a * StringReader. In case the string is base 64 encoded it will be decoded first. * */ public static LicenseReference createFrom(final String ref) { return createFrom(new StringReader(StringUtils.decodeBase64(ref))); }
/** * @return license reference element as Base64 encoded string */ @Override public String getReferenceAsBase64EncodedString() { return StringUtils.encodeBase64(referenceAsString()); }
@Override public String readRawResponse(HTTPResponse<?> response, InputStream rawStream) throws IOException { String charset = response.getContentCharset(); if (charset.isEmpty()) { charset = getDefaultCharset(); } return StringUtils.streamToString(rawStream, charset); }
@Override public InputStream asStream(String mimeType) { String charset = StringUtils.getCharsetFromContentType(mimeType); if (charset == null) { charset = defaultCharset; } try { return new StringInputStream(content, charset); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("unknown encoding: " + e); } } }
/** * joins an array of {@link Object}s, separated by a {@link String} * * @param parts the parts to join * @param separator the {@link String} to separates the elements * @return a {@link String} of all elements, separated by {@code separator} */ public static String join(String separator, String... parts) { return join(parts == null ? null : Arrays.asList(parts), separator); }
public static String streamToString(InputStream is, String charset) { try { return getStreamAsString(is, charset); } catch (IOException ex) { throw new IllegalStateException("Cannot convert stream to string", ex); } }
@Override protected String getCharacterEncoding() { String contentTypeHeaderValue = m_httpExchange.getRequestHeaders().getFirst(HttpHeaderAttribute.HEADER_NAME_CONTENTTYPE); if (contentTypeHeaderValue == null) { return null; } return StringUtils.getCharsetFromContentType(contentTypeHeaderValue); }
/** * constructs the string representation of the path. * * @return a string. */ public String asString() { if (size() == 1 && hasTrailingSlash()) { // special case if only one part, then join will not append the delimter. return PATH_DELIMITER; } return StringUtils.join(segments, PATH_DELIMITER); }
private Token parseToken(String parameter) { String tokenString = parameter; if (m_decodeBase64) { tokenString = StringUtils.decodeBase64(parameter); } return Token.parse(tokenString); }
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); } }
/** * Sets http header for basic authentication. Authentication will only be * set if username != null && !username.isEmpty() * * @param username * @param password * @return */ @Override public HTTPContentRequest basicAuth(String username, char[] password) { if (username != null && !username.isEmpty()) { setHeader(HTTPHeader.AUTHORIZATION.getName(), "Basic " + StringUtils.encodeBase64( new StringBuilder() .append(username) .append(":") .append(password) .toString())); } return this; }
public String getUsername() { try { byte[] ticketArray = m_credentialText.getBytes("UTF-8"); SAMLTicket ticket; if (Base64.isArrayByteBase64(ticketArray)) { ticket = new SAMLTicket(StringUtils.decodeBase64(m_credentialText)); Subject subject = ticket.asSubject(""); for (Principal principal : subject.getPrincipals()) { if (principal instanceof UsernameIDPrincipal) { return ((UsernameIDPrincipal) principal).getName(); } } String ticketAsString = ticket.asString(); return (ticketAsString.length() > 40) ? ticketAsString.substring(0, 40) : ticketAsString; } } catch (UnsupportedEncodingException e) { // do nothing -> return ticket "as is" } catch (SAMLException e) { // do nothing -> return ticket "as is" } return m_credentialText; }
} else { String msg = StringUtils.streamToString(httpResponse.getContent(), "UTF-8"); throw new DecisionProcessingException("PDP Service returned fault (httpcode:" + httpResponse.getStatus().getCode() + " msg:" + msg);
public String toBase64() { return StringUtils.encodeBase64(encodeToken().toString()); }
public AuthenticationContext authenticate(ServletRequestResponseContext requestCtx, AuthenticationService authService) throws AuthenticationException { HttpServletRequest request = requestCtx.getRequest(); String method = request.getMethod(); if ("GET".equals(method) && isDisableHttpGet()) { LOG.warn("Base64 credential login via HTTP GET not supported."); return null; } String cred = ServletUtils.getParameterValueEqualIgnoreCase(request, getCredentialParameterName()); if (cred == null || cred.isEmpty()) { return null; } String user; String pw; String authorizationString = StringUtils.decodeBase64(cred); int colon = authorizationString.indexOf(':'); if (colon < 0) { user = authorizationString; pw = null; } else { user = authorizationString.substring(0, colon); pw= authorizationString.substring(colon + 1); } CredentialsCallbackHandler callbackHandler = new CredentialsCallbackHandler(); callbackHandler.add(new UsernamePasswordCredential(user, pw)); callbackHandler.add(new IP4AddressCredential(request.getRemoteAddr())); return authService.login(callbackHandler); } };
/** * 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()); } }
String password = null; String cred = authorizationHeaderValue.substring(6); String authorizationString = StringUtils.decodeBase64(cred);