public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
public List<Header> formatCookies(final List<Cookie> cookies) { if (cookies == null) { throw new IllegalArgumentException("List of cookies may not be null"); } if (cookies.isEmpty()) { throw new IllegalArgumentException("List of cookies may not be empty"); } CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); buffer.append("="); String s = cookie.getValue(); if (s != null) { buffer.append(s); } } List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
/** * 处理返回结果数据 * * @param unitTest * @param response * @throws IOException */ private static void doResponse(UnitTest unitTest, CloseableHttpResponse response) throws IOException { int statusCode = response.getStatusLine().getStatusCode(); unitTest.setResponseCode(statusCode); StringBuffer sb = new StringBuffer(); for (int loop = 0; loop < response.getAllHeaders().length; loop++) { BufferedHeader header = (BufferedHeader) response .getAllHeaders()[loop]; if (header.getName().equals("Accept-Charset")) { continue; } sb.append(header.getName() + ":" + header.getValue() + "<br/>"); } unitTest.setResponseHeader(sb.toString()); HttpEntity entity = response.getEntity(); String result; if (entity != null) { result = EntityUtils.toString(entity, "utf-8"); unitTest.setResponseSize(result.getBytes().length); unitTest.setResponseBody(result); } EntityUtils.consume(entity); response.close(); }
/** * Returns a Diadoc <tt>Authorization</tt> header value for the given * {@link DiadocCredentials}. * * @param credentials The credentials to encode. * @return a Diadoc authorization header */ public static Header authenticate(final DiadocCredentials credentials) { if (credentials == null) { throw new IllegalArgumentException("Credentials may not be null"); } StringBuilder sb = new StringBuilder(); sb.append("ddauth_api_client_id="); sb.append(credentials.getApiClientId()); if (credentials.getAuthToken() != null) { sb.append(",ddauth_token="); sb.append(credentials.getAuthToken()); } CharArrayBuffer buffer = new CharArrayBuffer(128); buffer.append(AUTH.WWW_AUTH_RESP); buffer.append(": DiadocAuth "); buffer.append(sb.toString()); return new BufferedHeader(buffer); }
public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); final String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } buffer.append(cookie.getName()); final String s = cookie.getValue(); if (s != null) { buffer.append("="); buffer.append(s); } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
@Override public Header authenticate(Credentials credentials, HttpRequest request, HttpContext context) throws AuthenticationException { Args.notNull(credentials, "Credentials"); Args.notNull(request, "HTTP request"); // the bearer token is stored in the password field, not credentials.getUserPrincipal().getName() String bearerToken = credentials.getPassword(); CharArrayBuffer buffer = new CharArrayBuffer(64); if (isProxy()) { buffer.append("Proxy-Authorization"); } else { buffer.append("Authorization"); } buffer.append(": Bearer "); buffer.append(bearerToken); return new BufferedHeader(buffer); }
public List<Header> buildHeaders() { ArrayList<Header> headersArr = new ArrayList<>(); if (!StringUtils.isEmpty(headers)) { BufferedReader in = new BufferedReader(new StringReader(headers)); String str; try { while ((str = in.readLine()) != null) { CharArrayBuffer charArrayBuffer = new CharArrayBuffer(str.length()); charArrayBuffer.append(str); headersArr.add(new BufferedHeader(charArrayBuffer)); } } catch (IOException e) { throw new IllegalArgumentException(e); } } if (entityContentType != null) { headersArr.add(entityContentType); } else if (contentType != null && !contentType.toString().isEmpty()) { headersArr.add(new BasicHeader("Content-Type", contentType.toString())); } return headersArr; }
@Test public void build() { ContentType contentType = ContentType.parse(CONTENT_TYPE); List<Header> headers = new HeadersBuilder() .setHeaders("header1:value1\nheader2:value2") .setContentType(contentType) .buildHeaders(); assertEquals(3, headers.size()); assertThat(headers.get(0), instanceOf(BufferedHeader.class)); BufferedHeader basicHeader = (BufferedHeader) headers.get(1); assertEquals("header2", basicHeader.getName()); assertEquals("value2", basicHeader.getValue()); BasicHeader contentTypeHeader = (BasicHeader) headers.get(2); assertEquals("Content-Type", contentTypeHeader.getName()); assertEquals(CONTENT_TYPE, contentTypeHeader.getValue()); }
@Override public Header getVersionHeader() { CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append(SM.COOKIE2); buffer.append(": "); buffer.append("$Version="); buffer.append(Integer.toString(getVersion())); return new BufferedHeader(buffer); }
private List<Header> doFormatManyHeaders(final List<Cookie> cookies) { List<Header> headers = new ArrayList<Header>(cookies.size()); for (Cookie cookie : cookies) { int version = cookie.getVersion(); CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append("Cookie: "); buffer.append("$Version="); buffer.append(Integer.toString(version)); buffer.append("; "); formatCookieAsVer(buffer, cookie, version); headers.add(new BufferedHeader(buffer)); } return headers; }
/** * Returns a basic <tt>Authorization</tt> header value for the given * {@link Credentials} and charset. * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization header */ public static Header authenticate( final Credentials credentials, final String charset, boolean proxy) { if (credentials == null) { throw new IllegalArgumentException("Credentials may not be null"); } if (charset == null) { throw new IllegalArgumentException("charset may not be null"); } StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); byte[] base64password = Base64.encodeBase64( EncodingUtils.getBytes(tmp.toString(), charset)); CharArrayBuffer buffer = new CharArrayBuffer(32); if (proxy) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Basic "); buffer.append(base64password, 0, base64password.length); return new BufferedHeader(buffer); }
@Override public Header getVersionHeader() { final CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append(SM.COOKIE2); buffer.append(": "); buffer.append("$Version="); buffer.append(Integer.toString(getVersion())); return new BufferedHeader(buffer); }
private List<Header> doFormatManyHeaders(final List<Cookie> cookies) { final List<Header> headers = new ArrayList<Header>(cookies.size()); for (final Cookie cookie : cookies) { final int version = cookie.getVersion(); final CharArrayBuffer buffer = new CharArrayBuffer(40); buffer.append("Cookie: "); buffer.append("$Version="); buffer.append(Integer.toString(version)); buffer.append("; "); formatCookieAsVer(buffer, cookie, version); headers.add(new BufferedHeader(buffer)); } return headers; }
public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } final String cookieName = cookie.getName(); final String cookieValue = cookie.getValue(); if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) { BasicHeaderValueFormatterHC4.INSTANCE.formatHeaderElement( buffer, new BasicHeaderElement(cookieName, cookieValue), false); } else { // Netscape style cookies do not support quoted values buffer.append(cookieName); buffer.append("="); if (cookieValue != null) { buffer.append(cookieValue); } } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
/** * Produces basic authorization header for the given set of {@link Credentials}. * * @param credentials The set of credentials to be used for authentication * @param request The request being authenticated * @throws org.apache.http.auth.InvalidCredentialsException if authentication * credentials are not valid or not applicable for this authentication scheme * @throws AuthenticationException if authorization string cannot * be generated due to an authentication failure * * @return a basic authorization string */ @Override public Header authenticate( final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException { Args.notNull(credentials, "Credentials"); Args.notNull(request, "HTTP request"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final byte[] base64password = Base64.decode( EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request)), Base64.NO_WRAP); final CharArrayBuffer buffer = new CharArrayBuffer(32); if (isProxy()) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Basic "); buffer.append(base64password, 0, base64password.length); return new BufferedHeader(buffer); }
/** * Returns a basic <tt>Authorization</tt> header value for the given * {@link Credentials} and charset. * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization header * * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}. */ @Deprecated public static Header authenticate( final Credentials credentials, final String charset, final boolean proxy) { Args.notNull(credentials, "Credentials"); Args.notNull(charset, "charset"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final byte[] base64password = Base64.encode( EncodingUtils.getBytes(tmp.toString(), charset), Base64.NO_WRAP); final CharArrayBuffer buffer = new CharArrayBuffer(32); if (proxy) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Basic "); buffer.append(base64password, 0, base64password.length); return new BufferedHeader(buffer); }
@Override public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.size()); buffer.append(SM.COOKIE); buffer.append(": "); for (int i = 0; i < cookies.size(); i++) { final Cookie cookie = cookies.get(i); if (i > 0) { buffer.append("; "); } final String cookieName = cookie.getName(); final String cookieValue = cookie.getValue(); if (cookie.getVersion() > 0 && !isQuoteEnclosed(cookieValue)) { BasicHeaderValueFormatter.INSTANCE.formatHeaderElement( buffer, new BasicHeaderElement(cookieName, cookieValue), false); } else { // Netscape style cookies do not support quoted values buffer.append(cookieName); buffer.append("="); if (cookieValue != null) { buffer.append(cookieValue); } } } final List<Header> headers = new ArrayList<Header>(1); headers.add(new BufferedHeader(buffer)); return headers; }
/** * Produces basic authorization header for the given set of {@link Credentials}. * * @param credentials The set of credentials to be used for authentication * @param request The request being authenticated * @throws org.apache.http.auth.InvalidCredentialsException if authentication * credentials are not valid or not applicable for this authentication scheme * @throws AuthenticationException if authorization string cannot * be generated due to an authentication failure * * @return a basic authorization string */ @Override public Header authenticate( final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException { Args.notNull(credentials, "Credentials"); Args.notNull(request, "HTTP request"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final Base64 base64codec = new Base64(0); final byte[] base64password = base64codec.encode( EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request))); final CharArrayBuffer buffer = new CharArrayBuffer(32); if (isProxy()) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Basic "); buffer.append(base64password, 0, base64password.length); return new BufferedHeader(buffer); }
/** * Returns a basic {@code Authorization} header value for the given * {@link Credentials} and charset. * * @param credentials The credentials to encode. * @param charset The charset to use for encoding the credentials * * @return a basic authorization header * * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}. */ @Deprecated public static Header authenticate( final Credentials credentials, final String charset, final boolean proxy) { Args.notNull(credentials, "Credentials"); Args.notNull(charset, "charset"); final StringBuilder tmp = new StringBuilder(); tmp.append(credentials.getUserPrincipal().getName()); tmp.append(":"); tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword()); final byte[] base64password = Base64.encodeBase64( EncodingUtils.getBytes(tmp.toString(), charset), false); final CharArrayBuffer buffer = new CharArrayBuffer(32); if (proxy) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Basic "); buffer.append(base64password, 0, base64password.length); return new BufferedHeader(buffer); }
@Test public void testProcessChallengeWithLotsOfBlanks() throws Exception { final TestAuthScheme authscheme = new TestAuthScheme(); final CharArrayBuffer buffer = new CharArrayBuffer(32); buffer.append(" WWW-Authenticate: Test realm=\"realm1\""); final Header header = new BufferedHeader(buffer); authscheme.processChallenge(header); Assert.assertEquals("test", authscheme.getSchemeName()); Assert.assertEquals("realm1", authscheme.getParameter("realm")); }
@Override public Header authenticate(Credentials dummyCredentials, HttpRequest request, HttpContext context) throws AuthenticationException { TokenProvider tokenProvider = ContextHelper.get().beanForType(TokenProvider.class); String token = tokenProvider.getToken(getParameters(), request.getRequestLine().getMethod(), request.getRequestLine().getUri(), realCredentials); final CharArrayBuffer buffer = new CharArrayBuffer(32); buffer.append(AUTH.WWW_AUTH_RESP); buffer.append(": Bearer "); buffer.append(token); return new BufferedHeader(buffer); }