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());
}
}
java类org.apache.http.util.Args的实例源码
LocalRestRequest.java 文件源码
项目:Elasticsearch
阅读 18
收藏 0
点赞 0
评论 0
DefaultClientConnectionOperator.java 文件源码
项目:purecloud-iot
阅读 20
收藏 0
点赞 0
评论 0
@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);
}
DefaultConnectionKeepAliveStrategy.java 文件源码
项目:axon-eventstore
阅读 18
收藏 0
点赞 0
评论 0
@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;
}
BasicRouteDirector.java 文件源码
项目:purecloud-iot
阅读 16
收藏 0
点赞 0
评论 0
/**
* 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;
}
Scheme.java 文件源码
项目:purecloud-iot
阅读 22
收藏 0
点赞 0
评论 0
/**
* 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;
}
DefaultConnectionKeepAliveStrategyHC4.java 文件源码
项目:remote-files-sync
阅读 17
收藏 0
点赞 0
评论 0
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;
}
CacheConfig.java 文件源码
项目:purecloud-iot
阅读 19
收藏 0
点赞 0
评论 0
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());
}
RFC2965DomainAttributeHandlerHC4.java 文件源码
项目:remote-files-sync
阅读 23
收藏 0
点赞 0
评论 0
/**
* 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;
}
AbstractConnPool.java 文件源码
项目:remote-files-sync
阅读 32
收藏 0
点赞 0
评论 0
/**
* 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();
}
}
});
}
AuthenticationStrategyImpl.java 文件源码
项目:purecloud-iot
阅读 22
收藏 0
点赞 0
评论 0
@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);
}
}
AbstractPoolEntry.java 文件源码
项目:purecloud-iot
阅读 21
收藏 0
点赞 0
评论 0
/**
* 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());
}
BasicHttpClientConnectionManager.java 文件源码
项目:remote-files-sync
阅读 23
收藏 0
点赞 0
评论 0
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();
}
}
}
PublicSuffixMatcher.java 文件源码
项目:purecloud-iot
阅读 18
收藏 0
点赞 0
评论 0
/**
* @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);
}
}
}
}
TaskClientImpl.java 文件源码
项目:ibm-bpm-rest-client
阅读 20
收藏 0
点赞 0
评论 0
/**
* {@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>>() {});
}
RFC2965DomainAttributeHandler.java 文件源码
项目:purecloud-iot
阅读 26
收藏 0
点赞 0
评论 0
/**
* 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);
}
NetscapeDraftSpecHC4.java 文件源码
项目:remote-files-sync
阅读 18
收藏 0
点赞 0
评论 0
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;
}
DefaultCookieSpec.java 文件源码
项目:purecloud-iot
阅读 24
收藏 0
点赞 0
评论 0
@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);
}
}
RequestExpectContinue.java 文件源码
项目:purecloud-iot
阅读 26
收藏 0
点赞 0
评论 0
@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);
}
}
}
}
}
RFC2965PortAttributeHandlerHC4.java 文件源码
项目:remote-files-sync
阅读 21
收藏 0
点赞 0
评论 0
/**
* 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;
}
DatarouterHttpRequest.java 文件源码
项目:datarouter
阅读 20
收藏 0
点赞 0
评论 0
/**
* 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<>();
}
MinimalHttpClient.java 文件源码
项目:purecloud-iot
阅读 20
收藏 0
点赞 0
评论 0
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.java 文件源码
项目:purecloud-iot
阅读 16
收藏 0
点赞 0
评论 0
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");
}
Proxies.java 文件源码
项目:purecloud-iot
阅读 23
收藏 0
点赞 0
评论 0
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));
}
}
BasicHttpEntityHC4.java 文件源码
项目:remote-files-sync
阅读 19
收藏 0
点赞 0
评论 0
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();
}
}
ByteArrayEntityHC4.java 文件源码
项目:remote-files-sync
阅读 18
收藏 0
点赞 0
评论 0
/**
* @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());
}
}
ConnManagerParams.java 文件源码
项目:purecloud-iot
阅读 24
收藏 0
点赞 0
评论 0
/**
* 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;
}
SerializableEntityHC4.java 文件源码
项目:remote-files-sync
阅读 15
收藏 0
点赞 0
评论 0
/**
* 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;
}
}
SSLConnectionSocketFactory.java 文件源码
项目:purecloud-iot
阅读 26
收藏 0
点赞 0
评论 0
@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);
}
}
BackoffStrategyExec.java 文件源码
项目:purecloud-iot
阅读 23
收藏 0
点赞 0
评论 0
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;
}
PoolEntry.java 文件源码
项目:remote-files-sync
阅读 19
收藏 0
点赞 0
评论 0
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);
}