List of usage examples for org.apache.http.util Args notEmpty
public static <E, T extends Collection<E>> T notEmpty(T t, String str)
From source file:com.github.tomakehurst.wiremock.testsupport.MultipartBody.java
MultipartBody(String name, byte[] body) { super(ContentType.APPLICATION_OCTET_STREAM); Args.notEmpty(name, "Name was empty"); Args.notNull(body, "Body was null"); this.name = name; this.body = body; }
From source file:com.github.tomakehurst.wiremock.testsupport.MultipartBody.java
MultipartBody(String name, String body, ContentType contentType) { super(contentType); Args.notEmpty(name, "Name was empty"); Args.notEmpty(body, "Body was null"); this.name = name; this.body = bytesFromString(body, contentType.getCharset()); }
From source file:com.wudaosoft.net.httpclient.SSLContextBuilder.java
public SSLContext buildPKCS12() { Args.notEmpty(password, "password"); Args.notNull(cert, "cert"); char[] pwd = password.toCharArray(); try {//from w ww.ja v a2 s . com KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(cert.openStream(), pwd); // & ? KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(ks, pwd); // SSLContext SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(kmf.getKeyManagers(), null, new SecureRandom()); return sslContext; } catch (Exception e) { if (e instanceof RuntimeException) throw (RuntimeException) e; throw new RuntimeException(e); } }
From source file:cn.aage.robot.http.entity.ContentType.java
/** * @since 4.3/*from w w w. ja va 2 s . c om*/ */ public String getParameter(final String name) { Args.notEmpty(name, "Parameter name"); if (this.params == null) { return null; } for (final NameValuePair param : this.params) { if (param.getName().equalsIgnoreCase(name)) { return param.getValue(); } } return null; }
From source file:io.apiman.gateway.platforms.servlet.connectors.ssl.SSLSessionStrategyFactory.java
/** * Convenience function parses map of options to generate {@link SSLSessionStrategy}. * <p>/*from w ww . ja va2 s.c om*/ * Defaults are provided for some fields, others are options. ClientKeystore is required: * <p> * <ul> * <li>trustStore - default: <a href="https://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#CustomizingStores">JSSERefGuide</a></li> * <li>trustStorePassword - none</li> * <li>keyStore - required</li> * <li>keyStorePassword - none</li> * <li>keyAliases - none</li> * <li>keyPassword - none</li> * <li>allowedProtocols - {@link SSLParameters#getProtocols()}</li> * <li>disallowedProtocols - {@link SSLParameters#getCipherSuites()}</li> * <li>allowedCiphers - {@link SSLParameters#getCipherSuites()}</li> * <li>disallowedCiphers - {@link SSLParameters#getCipherSuites()}</li> * <li>allowAnyHost - false</li> * <li>allowSelfSigned - false</li> * </ul> * * @param optionsMap map of options * @return the SSL session strategy * @see #build(String, String, String, String, String[], String, String[], String[], boolean, boolean) * @throws NoSuchAlgorithmException if the selected algorithm is not available on the system * @throws KeyManagementException when particular cryptographic algorithm not available * @throws KeyStoreException problem with keystore * @throws CertificateException if there was a problem with the certificate * @throws IOException if the truststore could not be found or was invalid * @throws UnrecoverableKeyException a key in keystore cannot be recovered */ @SuppressWarnings("nls") public static SSLSessionStrategy buildMutual(TLSOptions optionsMap) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, UnrecoverableKeyException { Args.notNull(optionsMap.getKeyStore(), "KeyStore"); Args.notEmpty(optionsMap.getKeyStore(), "KeyStore must not be empty"); String[] allowedProtocols = arrayDifference(optionsMap.getAllowedProtocols(), optionsMap.getDisallowedProtocols(), getDefaultProtocols()); String[] allowedCiphers = arrayDifference(optionsMap.getAllowedCiphers(), optionsMap.getDisallowedCiphers(), getDefaultCipherSuites()); return build(optionsMap.getTrustStore(), optionsMap.getTrustStorePassword(), optionsMap.getKeyStore(), optionsMap.getKeyStorePassword(), optionsMap.getKeyAliases(), optionsMap.getKeyPassword(), allowedProtocols, allowedCiphers, optionsMap.isAllowAnyHost(), optionsMap.isTrustSelfSigned()); }
From source file:me.ixfan.wechatkit.user.UserManager.java
/** * ?????100?/*from w ww .j a va 2 s . c o m*/ * @param openIds OpenId * @param lang zh_CN zh_TW ?en zh-CN * @return {@link WeChatFollower}s * @throws WeChatApiErrorException API?? */ public List<WeChatFollower> batchGetUserInfo(final List<String> openIds, String lang) throws WeChatApiErrorException { Args.notEmpty(openIds, "OpenIds"); if (TextUtils.isBlank(lang)) { lang = "zh_CN"; } Args.check(lang.equals("zh_CN") || lang.equals("zh_TW") || lang.equals("en"), "'lang' ? zh_CN, zh_TW en."); final String finalLang = lang; JsonObject jsonResponse; try { final List<Map<String, String>> data = new ArrayList<>(); openIds.forEach(openid -> { Map<String, String> map = new HashMap<>(); map.put("openid", openid); map.put("lang", finalLang); data.add(map); }); final Map<String, List<Map<String, String>>> jsonMap = new HashMap<>(); jsonMap.put("user_list", data); Gson gson = new Gson(); jsonResponse = HttpClientUtil .sendPostRequestWithJsonBody(WeChatConstants.WECHAT_POST_BATCH_GET_USER_INFO .replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()), gson.toJson(jsonMap)); } catch (IOException e) { throw new RuntimeException(e); } if (jsonResponse.has("user_info_list")) { return WeChatFollower.batchFromJson(jsonResponse.get("user_info_list").getAsJsonArray()); } else { throw new WeChatApiErrorException(jsonResponse.get("errcode").getAsInt(), jsonResponse.get("errmsg").getAsString()); } }
From source file:me.ixfan.wechatkit.user.UserManager.java
/** * //from w w w . ja va 2 s. com * * @param tagName ??30 * @return {@link UserTag} * @throws WeChatApiErrorException API?? */ public UserTag createTag(String tagName) throws WeChatApiErrorException { Args.notEmpty(tagName, "Tag name"); final String url = WeChatConstants.WECHAT_POST_CREATE_TAG.replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()); final String jsonData = "{\"tag\":{\"name\":\"" + tagName + "\"}}"; JsonObject jsonResponse; try { jsonResponse = HttpClientUtil.sendPostRequestWithJsonBody(url, jsonData); } catch (IOException e) { throw new RuntimeException(e); } if (jsonResponse.has("tag")) { return new UserTag(jsonResponse.getAsJsonObject("tag").get("id").getAsInt(), jsonResponse.getAsJsonObject("tag").get("name").getAsString()); } else { throw new WeChatApiErrorException(jsonResponse.get("errcode").getAsInt(), jsonResponse.get("errmsg").getAsString()); } }
From source file:com.wudaosoft.net.httpclient.Request.java
/** * //from ww w .j a v a2s. c o m * @param workerBuilder * @param responseHandler * @return * @throws Exception */ public <T> T doRequest(WorkerBuilder workerBuilder, ResponseHandler<T> responseHandler) throws Exception { String method = workerBuilder.getMethod(); String url = workerBuilder.getUrl(); Args.notNull(workerBuilder, "WorkerBuilder"); Args.notEmpty(method, "WorkerBuilder.getMethod()"); Args.notEmpty(url, "WorkerBuilder.getUrl()"); Args.notNull(responseHandler, "responseHandler"); // if(!workerBuilder.isAnyHost()) { if (!isFullUrl(url)) { // notFullUrl(url); Args.notEmpty(hostConfig.getHostUrl(), "HostConfig.getHostUrl()"); url = hostConfig.getHostUrl() + url; } Charset charset = hostConfig.getCharset() == null ? Consts.UTF_8 : hostConfig.getCharset(); String stringBody = workerBuilder.getStringBody(); File fileBody = workerBuilder.getFileBody(); InputStream streamBody = workerBuilder.getStreamBody(); Map<String, String> params = workerBuilder.getParameters(); String contentType = null; if (responseHandler instanceof JsonResponseHandler) { contentType = MediaType.APPLICATION_JSON_VALUE; } else if (responseHandler instanceof SAXSourceResponseHandler || responseHandler instanceof XmlResponseHandler) { contentType = MediaType.APPLICATION_XML_VALUE; } else if (responseHandler instanceof FileResponseHandler || responseHandler instanceof ImageResponseHandler || responseHandler instanceof OutputStreamResponseHandler) { contentType = MediaType.ALL_VALUE; } else if (responseHandler instanceof NoResultResponseHandler) { contentType = ((NoResultResponseHandler) responseHandler).getContentType().getMimeType(); } else { contentType = MediaType.TEXT_PLAIN_VALUE; } RequestBuilder requestBuilder = RequestBuilder.create(method).setCharset(charset).setUri(url); if (stringBody != null) { StringEntity reqEntity = new StringEntity(stringBody, charset); reqEntity.setContentType(contentType + ";charset=" + charset.name()); requestBuilder.setEntity(reqEntity); } else if (fileBody != null || streamBody != null) { String filename = workerBuilder.getFilename(); MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create().setLaxMode(); if (fileBody != null) { Args.check(fileBody.isFile(), "fileBody must be a file"); Args.check(fileBody.canRead(), "fileBody must be readable"); if (filename == null && streamBody == null) filename = fileBody.getName(); FileBody bin = new FileBody(fileBody, ContentType.APPLICATION_OCTET_STREAM, streamBody != null ? fileBody.getName() : filename); reqEntity.addPart(workerBuilder.getFileFieldName(), bin); } Args.notEmpty(filename, "filename"); if (streamBody != null) reqEntity.addBinaryBody(workerBuilder.getFileFieldName(), streamBody, ContentType.APPLICATION_OCTET_STREAM, filename); buildParameters(reqEntity, params, charset); requestBuilder.setEntity(reqEntity.build()); } if (fileBody == null && streamBody == null) { buildParameters(requestBuilder, params); } if (workerBuilder.getReadTimeout() > -1) { requestBuilder.setConfig(RequestConfig.copy(this.hostConfig.getRequestConfig()) .setSocketTimeout(workerBuilder.getReadTimeout()).build()); } HttpUriRequest httpRequest = ParameterRequestBuilder.build(requestBuilder); setAcceptHeader(httpRequest, contentType); if (workerBuilder.isAjax()) setAjaxHeader(httpRequest); HttpClientContext context = workerBuilder.getContext(); if (context == null) context = defaultHttpContext; T result = getHttpClient().execute(httpRequest, responseHandler, context); if (log.isDebugEnabled()) { log.debug(String.format("Send data to path:[%s]\"%s\". result: %s", method, url, result)); } return result; }
From source file:me.ixfan.wechatkit.user.UserManager.java
/** * // w ww . j ava 2 s . co m * * @param tagId ID * @param tagName ?? * @throws WeChatApiErrorException API?? */ public void updateTag(int tagId, String tagName) throws WeChatApiErrorException { Args.notNegative(tagId, "Tag ID"); Args.notEmpty(tagName, "Tag name"); final String url = WeChatConstants.WECHAT_POST_UPDATE_TAG.replace("${ACCESS_TOKEN}", super.tokenManager.getAccessToken()); final String jsonData = "{\"tag\":{\"id\":" + String.valueOf(tagId) + ",\"name\":\"" + tagName + "\"}}"; JsonObject jsonResp; try { jsonResp = HttpClientUtil.sendPostRequestWithJsonBody(url, jsonData); } catch (IOException e) { throw new RuntimeException(e); } if (0 != jsonResp.get("errcode").getAsInt()) { throw new WeChatApiErrorException(jsonResp.get("errcode").getAsInt(), jsonResp.get("errmsg").getAsString()); } }