public void setContent(final String source, final ContentType contentType) throws UnsupportedCharsetException { Args.notNull(source, "Source string"); Charset charset = contentType != null?contentType.getCharset():null; if(charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } try { this.content = new BytesArray(source.getBytes(charset.name())); } catch (UnsupportedEncodingException var) { throw new UnsupportedCharsetException(charset.name()); } if(contentType != null) { addHeader("Content-Type", contentType.toString()); } }
@Override public void updateSecureConnection( final OperatedClientConnection conn, final HttpHost target, final HttpContext context, final HttpParams params) throws IOException { Args.notNull(conn, "Connection"); Args.notNull(target, "Target host"); Args.notNull(params, "Parameters"); Asserts.check(conn.isOpen(), "Connection must be open"); final SchemeRegistry registry = getSchemeRegistry(context); final Scheme schm = registry.getScheme(target.getSchemeName()); Asserts.check(schm.getSchemeSocketFactory() instanceof SchemeLayeredSocketFactory, "Socket factory must implement SchemeLayeredSocketFactory"); final SchemeLayeredSocketFactory lsf = (SchemeLayeredSocketFactory) schm.getSchemeSocketFactory(); final Socket sock = lsf.createLayeredSocket( conn.getSocket(), target.getHostName(), schm.resolvePort(target.getPort()), params); prepareSocket(sock, context, params); conn.update(sock, target, lsf.isSecure(sock), params); }
@Override public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); final HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { final HeaderElement he = it.nextElement(); final String param = he.getName(); final String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (final NumberFormatException ignore) { LOGGER.warn("keep alive timeout could not be parsed: param=" + param + " value:" + value, ignore); } } } return DEFAULT_KEEP_ALIVE; }
/** * Provides the next step. * * @param plan the planned route * @param fact the currently established route, or * {@code null} if nothing is established * * @return one of the constants defined in this class, indicating * either the next step to perform, or success, or failure. * 0 is for success, a negative value for failure. */ @Override public int nextStep(final RouteInfo plan, final RouteInfo fact) { Args.notNull(plan, "Planned route"); int step = UNREACHABLE; if ((fact == null) || (fact.getHopCount() < 1)) { step = firstStep(plan); } else if (plan.getHopCount() > 1) { step = proxiedStep(plan, fact); } else { step = directStep(plan, fact); } return step; }
/** * Creates a new scheme. * Whether the created scheme allows for layered connections * depends on the class of {@code factory}. * * @param name the scheme name, for example "http". * The name will be converted to lowercase. * @param factory the factory for creating sockets for communication * with this scheme * @param port the default port for this scheme * * @deprecated (4.1) Use {@link #Scheme(String, int, SchemeSocketFactory)} */ @Deprecated public Scheme(final String name, final SocketFactory factory, final int port) { Args.notNull(name, "Scheme name"); Args.notNull(factory, "Socket factory"); Args.check(port > 0 && port <= 0xffff, "Port is invalid"); this.name = name.toLowerCase(Locale.ENGLISH); if (factory instanceof LayeredSocketFactory) { this.socketFactory = new SchemeLayeredSocketFactoryAdaptor( (LayeredSocketFactory) factory); this.layered = true; } else { this.socketFactory = new SchemeSocketFactoryAdaptor(factory); this.layered = false; } this.defaultPort = port; }
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) { Args.notNull(response, "HTTP response"); final HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { final HeaderElement he = it.nextElement(); final String param = he.getName(); final String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch(final NumberFormatException ignore) { } } } return -1; }
public static Builder copy(final CacheConfig config) { Args.notNull(config, "Cache config"); return new Builder() .setMaxObjectSize(config.getMaxObjectSize()) .setMaxCacheEntries(config.getMaxCacheEntries()) .setMaxUpdateRetries(config.getMaxUpdateRetries()) .setHeuristicCachingEnabled(config.isHeuristicCachingEnabled()) .setHeuristicCoefficient(config.getHeuristicCoefficient()) .setHeuristicDefaultLifetime(config.getHeuristicDefaultLifetime()) .setSharedCache(config.isSharedCache()) .setAsynchronousWorkersMax(config.getAsynchronousWorkersMax()) .setAsynchronousWorkersCore(config.getAsynchronousWorkersCore()) .setAsynchronousWorkerIdleLifetimeSecs(config.getAsynchronousWorkerIdleLifetimeSecs()) .setRevalidationQueueSize(config.getRevalidationQueueSize()) .setNeverCacheHTTP10ResponsesWithQueryString(config.isNeverCacheHTTP10ResponsesWithQuery()); }
/** * Match cookie domain attribute. */ public boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); final String host = origin.getHost().toLowerCase(Locale.ENGLISH); final String cookieDomain = cookie.getDomain(); // The effective host name MUST domain-match the Domain // attribute of the cookie. if (!domainMatch(host, cookieDomain)) { return false; } // effective host name minus domain must not contain any dots final String effectiveHostWithoutDomain = host.substring( 0, host.length() - cookieDomain.length()); return effectiveHostWithoutDomain.indexOf('.') == -1; }
/** * Closes connections that have been idle longer than the given period * of time and evicts them from the pool. * * @param idletime maximum idle time. * @param tunit time unit. */ public void closeIdle(final long idletime, final TimeUnit tunit) { Args.notNull(tunit, "Time unit"); long time = tunit.toMillis(idletime); if (time < 0) { time = 0; } final long deadline = System.currentTimeMillis() - time; enumAvailable(new PoolEntryCallback<T, C>() { public void process(final PoolEntry<T, C> entry) { if (entry.getUpdated() <= deadline) { entry.close(); } } }); }
@Override public void authSucceeded( final HttpHost authhost, final AuthScheme authScheme, final HttpContext context) { Args.notNull(authhost, "Host"); Args.notNull(authScheme, "Auth scheme"); Args.notNull(context, "HTTP context"); final HttpClientContext clientContext = HttpClientContext.adapt(context); if (isCachable(authScheme)) { AuthCache authCache = clientContext.getAuthCache(); if (authCache == null) { authCache = new BasicAuthCache(); clientContext.setAuthCache(authCache); } if (this.log.isDebugEnabled()) { this.log.debug("Caching '" + authScheme.getSchemeName() + "' auth scheme for " + authhost); } authCache.put(authhost, authScheme); } }
/** * Layers a protocol on top of an established tunnel. * * @param context the context for layering * @param params the parameters for layering * * @throws IOException in case of a problem */ public void layerProtocol(final HttpContext context, final HttpParams params) throws IOException { //@@@ is context allowed to be null? depends on operator? Args.notNull(params, "HTTP parameters"); Asserts.notNull(this.tracker, "Route tracker"); Asserts.check(this.tracker.isConnected(), "Connection not open"); Asserts.check(this.tracker.isTunnelled(), "Protocol layering without a tunnel not supported"); Asserts.check(!this.tracker.isLayered(), "Multiple protocol layering not supported"); // - collect the arguments // - call the operator // - update the tracking data // In this order, we can be sure that only a successful // layering on top of the connection will be tracked. final HttpHost target = tracker.getTargetHost(); connOperator.updateSecureConnection(this.connection, target, context, params); this.tracker.layerProtocol(this.connection.isSecure()); }
public synchronized void closeIdleConnections(final long idletime, final TimeUnit tunit) { Args.notNull(tunit, "Time unit"); if (this.isShutdown.get()) { return; } if (!this.leased) { long time = tunit.toMillis(idletime); if (time < 0) { time = 0; } final long deadline = System.currentTimeMillis() - time; if (this.updated <= deadline) { closeConnection(); } } }
/** * @since 4.5 */ public PublicSuffixMatcher(final Collection<PublicSuffixList> lists) { Args.notNull(lists, "Domain suffix lists"); this.rules = new ConcurrentHashMap<String, DomainType>(); this.exceptions = new ConcurrentHashMap<String, DomainType>(); for (final PublicSuffixList list: lists) { final DomainType domainType = list.getType(); final List<String> rules = list.getRules(); for (final String rule: rules) { this.rules.put(rule, domainType); } final List<String> exceptions = list.getExceptions(); if (exceptions != null) { for (final String exception: exceptions) { this.exceptions.put(exception, domainType); } } } }
/** * {@inheritDoc} * * @throws IllegalArgumentException {@inheritDoc} */ @Override public RestRootEntity<ServiceData> setTaskData(@Nonnull String tkiid, @Nonnull Map<String, Object> parameters) { tkiid = Args.notNull(tkiid, "Task id (tkiid)"); parameters = Args.notNull(parameters, "Variables (parameters)"); Args.notEmpty(parameters.keySet(), "Parameters names"); Args.notEmpty(parameters.values(), "Parameters values"); Gson gson = new GsonBuilder().setDateFormat(DATE_TIME_FORMAT).create(); String params = gson.toJson(parameters); URI uri = new SafeUriBuilder(rootUri).addPath(tkiid).addParameter(ACTION, ACTION_SET_DATA) .addParameter(PARAMS, params).build(); return makePost(httpClient, httpContext, uri, new TypeToken<RestRootEntity<ServiceData>>() {}); }
/** * Parse cookie domain attribute. */ @Override public void parse( final SetCookie cookie, final String domain) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); if (domain == null) { throw new MalformedCookieException( "Missing value for domain attribute"); } if (domain.trim().isEmpty()) { throw new MalformedCookieException( "Blank value for domain attribute"); } String s = domain; s = s.toLowerCase(Locale.ROOT); if (!domain.startsWith(".")) { // Per RFC 2965 section 3.2.2 // "... If an explicitly specified value does not start with // a dot, the user agent supplies a leading dot ..." // That effectively implies that the domain attribute // MAY NOT be an IP address of a host name s = '.' + s; } cookie.setDomain(s); }
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.notNull(cookies, "List of cookies"); int version = Integer.MAX_VALUE; boolean isSetCookie2 = true; for (final Cookie cookie: cookies) { if (!(cookie instanceof SetCookie2)) { isSetCookie2 = false; } if (cookie.getVersion() < version) { version = cookie.getVersion(); } } if (version > 0) { if (isSetCookie2) { return strict.formatCookies(cookies); } else { return obsoleteStrict.formatCookies(cookies); } } else { return netscapeDraft.formatCookies(cookies); } }
@Override public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException { Args.notNull(request, "HTTP request"); if (!request.containsHeader(HTTP.EXPECT_DIRECTIVE)) { if (request instanceof HttpEntityEnclosingRequest) { final ProtocolVersion ver = request.getRequestLine().getProtocolVersion(); final HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity(); // Do not send the expect header if request body is known to be empty if (entity != null && entity.getContentLength() != 0 && !ver.lessEquals(HttpVersion.HTTP_1_0)) { final HttpClientContext clientContext = HttpClientContext.adapt(context); final RequestConfig config = clientContext.getRequestConfig(); if (config.isExpectContinueEnabled()) { request.addHeader(HTTP.EXPECT_DIRECTIVE, HTTP.EXPECT_CONTINUE); } } } } }
/** * Match cookie port attribute. If the Port attribute is not specified * in header, the cookie can be sent to any port. Otherwise, the request port * must be in the cookie's port list. */ public boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); final int port = origin.getPort(); if (cookie instanceof ClientCookie && ((ClientCookie) cookie).containsAttribute(ClientCookie.PORT_ATTR)) { if (cookie.getPorts() == null) { // Invalid cookie state: port not specified return false; } if (!portMatch(port, cookie.getPorts())) { return false; } } return true; }
/** * Expects query string parameters to already be UTF-8 encoded. See AdvancedStringTool.makeUrlParameters(). * URL fragment is stripped from URL when sent to server. */ public DatarouterHttpRequest(HttpRequestMethod method, final String url, boolean retrySafe){ Args.notBlank(url, "request url"); Args.notNull(method, "http method"); String fragment; int fragmentIndex = url.indexOf('#'); if(fragmentIndex > 0 && fragmentIndex < url.length() - 1){ fragment = url.substring(fragmentIndex + 1); }else{ fragmentIndex = url.length(); fragment = ""; } String path = url.substring(0, fragmentIndex); Map<String,List<String>> queryParams; int queryIndex = path.indexOf("?"); if(queryIndex > 0){ queryParams = extractQueryParams(path.substring(queryIndex + 1)); path = path.substring(0, queryIndex); }else{ queryParams = new LinkedHashMap<>(); } this.method = method; this.path = path; this.retrySafe = retrySafe; this.fragment = fragment; this.headers = new HashMap<>(); this.queryParams = queryParams; this.postParams = new HashMap<>(); this.cookies = new ArrayList<>(); }
public MinimalHttpClient( final HttpClientConnectionManager connManager) { super(); this.connManager = Args.notNull(connManager, "HTTP connection manager"); this.requestExecutor = new MinimalClientExec( new HttpRequestExecutor(), connManager, DefaultConnectionReuseStrategy.INSTANCE, DefaultConnectionKeepAliveStrategy.INSTANCE); this.params = new BasicHttpParams(); }
ExponentialBackOffSchedulingStrategy( final ScheduledExecutorService executor, final long backOffRate, final long initialExpiryInMillis, final long maxExpiryInMillis) { this.executor = Args.notNull(executor, "Executor"); this.backOffRate = Args.notNegative(backOffRate, "BackOffRate"); this.initialExpiryInMillis = Args.notNegative(initialExpiryInMillis, "InitialExpiryInMillis"); this.maxExpiryInMillis = Args.notNegative(maxExpiryInMillis, "MaxExpiryInMillis"); }
public static CloseableHttpResponse enhanceResponse(final HttpResponse original) { Args.notNull(original, "HTTP response"); if (original instanceof CloseableHttpResponse) { return (CloseableHttpResponse) original; } else { return (CloseableHttpResponse) Proxy.newProxyInstance( ResponseProxyHandler.class.getClassLoader(), new Class<?>[] { CloseableHttpResponse.class }, new ResponseProxyHandler(original)); } }
public void writeTo(final OutputStream outstream) throws IOException { Args.notNull(outstream, "Output stream"); final InputStream instream = getContent(); try { int l; final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE]; while ((l = instream.read(tmp)) != -1) { outstream.write(tmp, 0, l); } } finally { instream.close(); } }
/** * @since 4.2 */ @SuppressWarnings("deprecation") public ByteArrayEntityHC4(final byte[] b, final ContentType contentType) { super(); Args.notNull(b, "Source byte array"); this.content = b; this.b = b; this.off = 0; this.len = this.b.length; if (contentType != null) { setContentType(contentType.toString()); } }
/** * Returns lookup interface for maximum number of connections allowed per route. * * @param params HTTP parameters * * @return lookup interface for maximum number of connections allowed per route. */ public static ConnPerRoute getMaxConnectionsPerRoute(final HttpParams params) { Args.notNull(params, "HTTP parameters"); ConnPerRoute connPerRoute = (ConnPerRoute) params.getParameter(MAX_CONNECTIONS_PER_ROUTE); if (connPerRoute == null) { connPerRoute = DEFAULT_CONN_PER_ROUTE; } return connPerRoute; }
/** * Creates new instance of this class. * * @param ser input * @param bufferize tells whether the content should be * stored in an internal buffer * @throws IOException in case of an I/O error */ public SerializableEntityHC4(final Serializable ser, final boolean bufferize) throws IOException { super(); Args.notNull(ser, "Source object"); if (bufferize) { createBytes(ser); } else { this.objRef = ser; } }
@Override public Socket connectSocket( final int connectTimeout, final Socket socket, final HttpHost host, final InetSocketAddress remoteAddress, final InetSocketAddress localAddress, final HttpContext context) throws IOException { Args.notNull(host, "HTTP host"); Args.notNull(remoteAddress, "Remote address"); final Socket sock = socket != null ? socket : createSocket(context); if (localAddress != null) { sock.bind(localAddress); } try { if (connectTimeout > 0 && sock.getSoTimeout() == 0) { sock.setSoTimeout(connectTimeout); } if (this.log.isDebugEnabled()) { this.log.debug("Connecting socket to " + remoteAddress + " with timeout " + connectTimeout); } sock.connect(remoteAddress, connectTimeout); } catch (final IOException ex) { try { sock.close(); } catch (final IOException ignore) { } throw ex; } // Setup SSL layering if necessary if (sock instanceof SSLSocket) { final SSLSocket sslsock = (SSLSocket) sock; this.log.debug("Starting handshake"); sslsock.startHandshake(); verifyHostname(sslsock, host.getHostName()); return sock; } else { return createLayeredSocket(sock, host.getHostName(), remoteAddress.getPort(), context); } }
public BackoffStrategyExec( final ClientExecChain requestExecutor, final ConnectionBackoffStrategy connectionBackoffStrategy, final BackoffManager backoffManager) { super(); Args.notNull(requestExecutor, "HTTP client request executor"); Args.notNull(connectionBackoffStrategy, "Connection backoff strategy"); Args.notNull(backoffManager, "Backoff manager"); this.requestExecutor = requestExecutor; this.connectionBackoffStrategy = connectionBackoffStrategy; this.backoffManager = backoffManager; }
public synchronized void updateExpiry(final long time, final TimeUnit tunit) { Args.notNull(tunit, "Time unit"); this.updated = System.currentTimeMillis(); final long newExpiry; if (time > 0) { newExpiry = this.updated + tunit.toMillis(time); } else { newExpiry = Long.MAX_VALUE; } this.expiry = Math.min(newExpiry, this.validUnit); }
@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; }
@Override public final boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: this.attribHandlers) { if (!handler.match(cookie, origin)) { return false; } } return true; }
public void setDefaultMaxPerRoute(final int max) { Args.notNegative(max, "Max per route value"); this.lock.lock(); try { this.defaultMaxPerRoute = max; } finally { this.lock.unlock(); } }
/** * Closes idle connections. * * @param idletime the time the connections should have been idle * in order to be closed now * @param tunit the unit for the {@code idletime} */ @Override public void closeIdleConnections(final long idletime, final TimeUnit tunit) { Args.notNull(tunit, "Time unit"); final long t = idletime > 0 ? idletime : 0; if (log.isDebugEnabled()) { log.debug("Closing connections idle longer than " + t + " " + tunit); } // the latest time for which connections will be closed final long deadline = System.currentTimeMillis() - tunit.toMillis(t); poolLock.lock(); try { final Iterator<BasicPoolEntry> iter = freeConnections.iterator(); while (iter.hasNext()) { final BasicPoolEntry entry = iter.next(); if (entry.getUpdated() <= deadline) { if (log.isDebugEnabled()) { log.debug("Closing connection last used @ " + new Date(entry.getUpdated())); } iter.remove(); deleteEntry(entry); } } } finally { poolLock.unlock(); } }
public RequestBuilder addParameter(final NameValuePair nvp) { Args.notNull(nvp, "Name value pair"); if (parameters == null) { parameters = new LinkedList<NameValuePair>(); } parameters.add(nvp); return this; }
public static ProtocolVersion parseProtocolVersion(final String value, final LineParser parser) throws ParseException { Args.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicLineParserHC4.INSTANCE) .parseProtocolVersion(buffer, cursor); }
public static RequestLine parseRequestLine(final String value, final LineParser parser) throws ParseException { Args.notNull(value, "Value"); final CharArrayBuffer buffer = new CharArrayBuffer(value.length()); buffer.append(value); final ParserCursor cursor = new ParserCursor(0, value.length()); return (parser != null ? parser : BasicLineParserHC4.INSTANCE) .parseRequestLine(buffer, cursor); }
/** * Creates new instance of AbstractMessageParserHC4. * * @param buffer the session input buffer. * @param lineParser the line parser. If <code>null</code> {@link BasicLineParserHC4#INSTANCE} * will be used. * @param constraints the message constraints. If <code>null</code> * {@link MessageConstraints#DEFAULT} will be used. * * @since 4.3 */ public AbstractMessageParserHC4( final SessionInputBuffer buffer, final LineParser lineParser, final MessageConstraints constraints) { super(); this.sessionBuffer = Args.notNull(buffer, "Session input buffer"); this.lineParser = lineParser != null ? lineParser : BasicLineParserHC4.INSTANCE; this.messageConstraints = constraints != null ? constraints : MessageConstraints.DEFAULT; this.headerLines = new ArrayList<CharArrayBuffer>(); this.state = HEAD_LINE; }
public HeaderElement parseHeader( final CharArrayBuffer buffer, final ParserCursor cursor) throws ParseException { Args.notNull(buffer, "Char array buffer"); Args.notNull(cursor, "Parser cursor"); final NameValuePair nvp = parseNameValuePair(buffer, cursor); final List<NameValuePair> params = new ArrayList<NameValuePair>(); while (!cursor.atEnd()) { final NameValuePair param = parseNameValuePair(buffer, cursor); params.add(param); } return new BasicHeaderElement( nvp.getName(), nvp.getValue(), params.toArray(new NameValuePair[params.size()])); }
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: getAttribHandlers()) { handler.validate(cookie, origin); } }