Example usage for org.apache.http.util Args notNull

List of usage examples for org.apache.http.util Args notNull

Introduction

In this page you can find the example usage for org.apache.http.util Args notNull.

Prototype

public static <T> T notNull(T t, String str) 

Source Link

Usage

From source file:me.ixfan.wechatkit.message.MessageManager.java

/**
 * ? OpenId ??/*from  www  .j  av  a  2s. c  o  m*/
 * @param mediaId ??? media_id
 * @param ignoreReprint <p>??? false</p>
 *                      <p>true  - ??</p>
 *                      <p>false - ???</p>
 * @param openIds OpenId 
 * @return <p>???? {@link WeChatApiResult#getMsgId()} ????ID
 *  {@link WeChatApiResult#getMsgDataId()} ???ID??????????????msgid??????msgid?</p>
 * <p>??? {@link WeChatApiResult#getErrcode()}  {@link WeChatApiResult#getErrmsg()}
 * ??</p>
 */
public WeChatApiResult sendMassArticleToUsers(String mediaId, boolean ignoreReprint, String... openIds) {
    Args.notEmpty(mediaId, "Media ID of news material");
    Args.notNull(openIds, "OpenId list");
    Args.notEmpty(Arrays.asList(openIds), "OpenId list");

    final String url = WeChatConstants.WECHAT_POST_MESSAGE_MASS_SNED_BY_OPENIDS.replace("${ACCESS_TOKEN}",
            super.getTokenManager().getAccessToken());
    MessageForMassSend msg = new MessageForMassSend(OutMessageType.MP_NEWS, mediaId, Arrays.asList(openIds));
    msg.setSendIgnoreReprint(ignoreReprint ? 1 : 0);
    try {
        JsonObject jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody(url, msg.toJsonString());
        return WeChatApiResult.instanceOf(jsonResponse);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.http.client.protocol.RequestAddCookies.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");

    final String method = request.getRequestLine().getMethod();
    if (method.equalsIgnoreCase("CONNECT")) {
        return;//  ww  w .  java2s.  co  m
    }

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    // Obtain cookie store
    final CookieStore cookieStore = clientContext.getCookieStore();
    if (cookieStore == null) {
        this.log.debug("Cookie store not specified in HTTP context");
        return;
    }

    // Obtain the registry of cookie specs
    final Lookup<CookieSpecProvider> registry = clientContext.getCookieSpecRegistry();
    if (registry == null) {
        this.log.debug("CookieSpec registry not specified in HTTP context");
        return;
    }

    // Obtain the target host, possibly virtual (required)
    final HttpHost targetHost = clientContext.getTargetHost();
    if (targetHost == null) {
        this.log.debug("Target host not set in the context");
        return;
    }

    // Obtain the route (required)
    final RouteInfo route = clientContext.getHttpRoute();
    if (route == null) {
        this.log.debug("Connection route not set in the context");
        return;
    }

    final RequestConfig config = clientContext.getRequestConfig();
    String policy = config.getCookieSpec();
    if (policy == null) {
        policy = CookieSpecs.BEST_MATCH;
    }
    if (this.log.isDebugEnabled()) {
        this.log.debug("CookieSpec selected: " + policy);
    }

    URI requestURI = null;
    if (request instanceof HttpUriRequest) {
        requestURI = ((HttpUriRequest) request).getURI();
    } else {
        try {
            requestURI = new URI(request.getRequestLine().getUri());
        } catch (final URISyntaxException ignore) {
        }
    }
    final String path = requestURI != null ? requestURI.getPath() : null;
    final String hostName = targetHost.getHostName();
    int port = targetHost.getPort();
    if (port < 0) {
        port = route.getTargetHost().getPort();
    }

    final CookieOrigin cookieOrigin = new CookieOrigin(hostName, port >= 0 ? port : 0,
            !TextUtils.isEmpty(path) ? path : "/", route.isSecure());

    // Get an instance of the selected cookie policy
    final CookieSpecProvider provider = registry.lookup(policy);
    if (provider == null) {
        throw new HttpException("Unsupported cookie policy: " + policy);
    }
    final CookieSpec cookieSpec = provider.create(clientContext);
    // Get all cookies available in the HTTP state
    final List<Cookie> cookies = new ArrayList<Cookie>(cookieStore.getCookies());
    // Find cookies matching the given origin
    final List<Cookie> matchedCookies = new ArrayList<Cookie>();
    final Date now = new Date();
    for (final Cookie cookie : cookies) {
        if (!cookie.isExpired(now)) {
            if (cookieSpec.match(cookie, cookieOrigin)) {
                if (this.log.isDebugEnabled()) {
                    this.log.debug("Cookie " + cookie + " match " + cookieOrigin);
                }
                matchedCookies.add(cookie);
            }
        } else {
            if (this.log.isDebugEnabled()) {
                this.log.debug("Cookie " + cookie + " expired");
            }
        }
    }
    // Generate Cookie request headers
    if (!matchedCookies.isEmpty()) {
        final List<Header> headers = cookieSpec.formatCookies(matchedCookies);
        for (final Header header : headers) {
            request.addHeader(header);
        }
    }

    final int ver = cookieSpec.getVersion();
    if (ver > 0) {
        boolean needVersionHeader = false;
        for (final Cookie cookie : matchedCookies) {
            if (ver != cookie.getVersion() || !(cookie instanceof SetCookie2)) {
                needVersionHeader = true;
            }
        }

        if (needVersionHeader) {
            final Header header = cookieSpec.getVersionHeader();
            if (header != null) {
                // Advertise cookie version support
                request.addHeader(header);
            }
        }
    }

    // Stick the CookieSpec and CookieOrigin instances to the HTTP context
    // so they could be obtained by the response interceptor
    context.setAttribute(HttpClientContext.COOKIE_SPEC, cookieSpec);
    context.setAttribute(HttpClientContext.COOKIE_ORIGIN, cookieOrigin);
}

From source file:org.apache.http.client.protocol.RequestAuthCache.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    Args.notNull(request, "HTTP request");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    final AuthCache authCache = clientContext.getAuthCache();
    if (authCache == null) {
        this.log.debug("Auth cache not set in the context");
        return;// w  w  w.ja  v a 2s.  co  m
    }

    final CredentialsProvider credsProvider = clientContext.getCredentialsProvider();
    if (credsProvider == null) {
        this.log.debug("Credentials provider not set in the context");
        return;
    }

    final RouteInfo route = clientContext.getHttpRoute();
    HttpHost target = clientContext.getTargetHost();
    if (target.getPort() < 0) {
        target = new HttpHost(target.getHostName(), route.getTargetHost().getPort(), target.getSchemeName());
    }

    final AuthState targetState = clientContext.getTargetAuthState();
    if (targetState != null && targetState.getState() == AuthProtocolState.UNCHALLENGED) {
        final AuthScheme authScheme = authCache.get(target);
        if (authScheme != null) {
            doPreemptiveAuth(target, authScheme, targetState, credsProvider);
        }
    }

    final HttpHost proxy = route.getProxyHost();
    final AuthState proxyState = clientContext.getProxyAuthState();
    if (proxy != null && proxyState != null && proxyState.getState() == AuthProtocolState.UNCHALLENGED) {
        final AuthScheme authScheme = authCache.get(proxy);
        if (authScheme != null) {
            doPreemptiveAuth(proxy, authScheme, proxyState, credsProvider);
        }
    }
}

From source file:org.apache.http.client.protocol.RequestClientConnControl.java

public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
    Args.notNull(request, "HTTP request");

    final String method = request.getRequestLine().getMethod();
    if (method.equalsIgnoreCase("CONNECT")) {
        request.setHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
        return;/*from  w ww.  j av  a  2s  .c o  m*/
    }

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    // Obtain the client connection (required)
    final RouteInfo route = clientContext.getHttpRoute();
    if (route == null) {
        this.log.debug("Connection route not set in the context");
        return;
    }

    if (route.getHopCount() == 1 || route.isTunnelled()) {
        if (!request.containsHeader(HTTP.CONN_DIRECTIVE)) {
            request.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
        }
    }
    if (route.getHopCount() == 2 && !route.isTunnelled()) {
        if (!request.containsHeader(PROXY_CONN_DIRECTIVE)) {
            request.addHeader(PROXY_CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
        }
    }
}

From source file:org.apache.http.client.protocol.ResponseAuthCache.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP request");
    Args.notNull(context, "HTTP context");
    AuthCache authCache = (AuthCache) context.getAttribute(ClientContext.AUTH_CACHE);

    HttpHost target = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
    final AuthState targetState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
    if (target != null && targetState != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Target auth state: " + targetState.getState());
        }//  ww  w .j  a  v  a2  s .  c  o  m
        if (isCachable(targetState)) {
            final SchemeRegistry schemeRegistry = (SchemeRegistry) context
                    .getAttribute(ClientContext.SCHEME_REGISTRY);
            if (target.getPort() < 0) {
                final Scheme scheme = schemeRegistry.getScheme(target);
                target = new HttpHost(target.getHostName(), scheme.resolvePort(target.getPort()),
                        target.getSchemeName());
            }
            if (authCache == null) {
                authCache = new BasicAuthCache();
                context.setAttribute(ClientContext.AUTH_CACHE, authCache);
            }
            switch (targetState.getState()) {
            case CHALLENGED:
                cache(authCache, target, targetState.getAuthScheme());
                break;
            case FAILURE:
                uncache(authCache, target, targetState.getAuthScheme());
            }
        }
    }

    final HttpHost proxy = (HttpHost) context.getAttribute(ExecutionContext.HTTP_PROXY_HOST);
    final AuthState proxyState = (AuthState) context.getAttribute(ClientContext.PROXY_AUTH_STATE);
    if (proxy != null && proxyState != null) {
        if (this.log.isDebugEnabled()) {
            this.log.debug("Proxy auth state: " + proxyState.getState());
        }
        if (isCachable(proxyState)) {
            if (authCache == null) {
                authCache = new BasicAuthCache();
                context.setAttribute(ClientContext.AUTH_CACHE, authCache);
            }
            switch (proxyState.getState()) {
            case CHALLENGED:
                cache(authCache, proxy, proxyState.getAuthScheme());
                break;
            case FAILURE:
                uncache(authCache, proxy, proxyState.getAuthScheme());
            }
        }
    }
}

From source file:org.apache.http.client.protocol.ResponseProcessCookies.java

public void process(final HttpResponse response, final HttpContext context) throws HttpException, IOException {
    Args.notNull(response, "HTTP request");
    Args.notNull(context, "HTTP context");

    final HttpClientContext clientContext = HttpClientContext.adapt(context);

    // Obtain actual CookieSpec instance
    final CookieSpec cookieSpec = clientContext.getCookieSpec();
    if (cookieSpec == null) {
        this.log.debug("Cookie spec not specified in HTTP context");
        return;/*  www .ja v a  2 s .c  om*/
    }
    // Obtain cookie store
    final CookieStore cookieStore = clientContext.getCookieStore();
    if (cookieStore == null) {
        this.log.debug("Cookie store not specified in HTTP context");
        return;
    }
    // Obtain actual CookieOrigin instance
    final CookieOrigin cookieOrigin = clientContext.getCookieOrigin();
    if (cookieOrigin == null) {
        this.log.debug("Cookie origin not specified in HTTP context");
        return;
    }
    HeaderIterator it = response.headerIterator(SM.SET_COOKIE);
    processCookies(it, cookieSpec, cookieOrigin, cookieStore);

    // see if the cookie spec supports cookie versioning.
    if (cookieSpec.getVersion() > 0) {
        // process set-cookie2 headers.
        // Cookie2 will replace equivalent Cookie instances
        it = response.headerIterator(SM.SET_COOKIE2);
        processCookies(it, cookieSpec, cookieOrigin, cookieStore);
    }
}

From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java

/**
 * @deprecated (4.4) Use {@link #SSLConnectionSocketFactory(javax.net.ssl.SSLContext,
 *   javax.net.ssl.HostnameVerifier)}/*w  w  w .  j  a v  a2s  .  c  om*/
 */
@Deprecated
public SSLConnectionSocketFactory(final SSLContext sslContext, final X509HostnameVerifier hostnameVerifier) {
    this(Args.notNull(sslContext, "SSL context").getSocketFactory(), null, null, hostnameVerifier);
}

From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java

/**
 * @deprecated (4.4) Use {@link #SSLConnectionSocketFactory(javax.net.ssl.SSLContext,
 *   String[], String[], javax.net.ssl.HostnameVerifier)}
 *//*ww w .ja  v a 2  s  . c  o m*/
@Deprecated
public SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols,
        final String[] supportedCipherSuites, final X509HostnameVerifier hostnameVerifier) {
    this(Args.notNull(sslContext, "SSL context").getSocketFactory(), supportedProtocols, supportedCipherSuites,
            hostnameVerifier);
}

From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java

/**
 * @since 4.4//from   w  w  w. j  a  v  a 2  s  .  c o m
 */
public SSLConnectionSocketFactory(final SSLContext sslContext, final HostnameVerifier hostnameVerifier) {
    this(Args.notNull(sslContext, "SSL context").getSocketFactory(), null, null, hostnameVerifier);
}

From source file:org.apache.http.conn.ssl.SSLConnectionSocketFactory.java

/**
 * @since 4.4//from w ww .j av a  2  s. co  m
 */
public SSLConnectionSocketFactory(final SSLContext sslContext, final String[] supportedProtocols,
        final String[] supportedCipherSuites, final HostnameVerifier hostnameVerifier) {
    this(Args.notNull(sslContext, "SSL context").getSocketFactory(), supportedProtocols, supportedCipherSuites,
            hostnameVerifier);
}