public Iterable<String> call(Iterator<String> input) { ArrayList<String> content = new ArrayList<String>(); ArrayList<ContentExchange> cea = new ArrayList<ContentExchange>(); HttpClient client = new HttpClient(); try { client.start(); while (input.hasNext()) { ContentExchange exchange = new ContentExchange(true); exchange.setURL("http://qrzcq.com/call/" + input.next()); client.send(exchange); cea.add(exchange); } for (ContentExchange exchange : cea) { exchange.waitForDone(); content.add(exchange.getResponseContent()); } } catch (Exception e) { } return content; }}); System.out.println(StringUtils.join(result.collect(), ","));
static CallLog[] readExchangeCallLog(ObjectMapper mapper, ContentExchange exchange) { try { exchange.waitForDone(); String responseJson = exchange.getResponseContent(); return mapper.readValue(responseJson, CallLog[].class); } catch (Exception e) { return new CallLog[0]; } }
static Tuple2<String, ContentExchange> createRequestForSign(String sign, HttpClient client) throws Exception { ContentExchange exchange = new ContentExchange(true); exchange.setURL("http://new73s.herokuapp.com/qsos/" + sign + ".json"); client.send(exchange); return new Tuple2(sign, exchange); } static ObjectMapper createMapper() {
ContentExchange exchange = new ContentExchange(); exchange.setMethod("POST"); exchange.setURL(url); exchange.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); exchange.setRequestContentSource(new ByteArrayInputStream(message .getBytes("UTF-8"))); exchange.waitForDone(); return new JSONObject(new JSONTokener(exchange.getResponseContent()));
/** * HTTP notifications will be set out to subscribers interested in resources from the tree where they have have hung * onem2m subscription resources * @param url where do i send this onem2m notify message * @param payload contents of the notification */ @Override public void sendNotification(String url, String payload) { ContentExchange ex = new ContentExchange(); ex.setURL(url); ex.setRequestContentSource(new ByteArrayInputStream(payload.getBytes())); ex.setRequestContentType(Onem2m.ContentType.APP_VND_NTFY_JSON); Integer cl = payload != null ? payload.length() : 0; ex.setRequestHeader("Content-Length", cl.toString()); ex.setMethod("post"); LOG.debug("HTTP: Send notification uri: {}, payload: {}:", url, payload); try { client.send(ex); } catch (IOException e) { LOG.error("Dropping notification: uri: {}, payload: {}", url, payload); } }
ContentExchange ex = new ContentExchange(true); ex.setMethod(translateOperationToMethod(request.getPrimitive(RequestPrimitive.OPERATION))); ex.setAddress(new Address(host.getHost(), host.getPort())); ex.setURI(url); ex.setRequestHeader(Onem2m.HttpHeaders.X_M2M_ORIGIN, request.getPrimitive(RequestPrimitive.FROM)); ex.setRequestHeader("Host", hostURL); ex.setRequestHeader(Onem2m.HttpHeaders.X_M2M_RI, request.getPrimitive(RequestPrimitive.REQUEST_IDENTIFIER)); ex.setRequestHeader(Onem2m.HttpHeaders.X_M2M_GID, request.getPrimitive(RequestPrimitive.GROUP_REQUEST_IDENTIFIER)); ex.setRequestHeader(Onem2m.HttpHeaders.X_M2M_OT, request.getPrimitive(RequestPrimitive.ORIGINATING_TIMESTAMP)); ex.setRequestContentSource(new ByteArrayInputStream( request.getPrimitive(RequestPrimitive.CONTENT).getBytes())); ex.setRequestContentType(createRequestContentTypeString(request));
@Override public Future<Document> execute() throws IOException { final SettableFuture<Document> future = SettableFuture.create(); ContentExchange exchange = new ContentExchange() { protected void onResponseComplete() throws IOException { super.onResponseComplete(); String responseContent = this.getResponseContent(); try { future.set(db.parse(new InputSource(responseContent))); } catch (SAXException e) { future.setException(e); } } }; exchange.setRequestHeader("Accept", "text/xml, application/xml"); exchange.setMethod("GET"); exchange.setURL(url.toExternalForm()); // start the exchange client.send(exchange); return future; } }
public String processRequest(String url, ContentExchange httpRequest) { ContentExchange httpResponse = sendRequest(url, httpRequest); try { String responseContent = httpResponse.getResponseContent(); int rsc = httpResponse.getResponseStatus(); LOG.info("handleRequest: responseStatus: {}, responseContent: {}", rsc, responseContent); if (rsc < 200 || rsc >= 300) { LOG.error("handleRequest: httpStatusCode: {} ...", rsc); return null; } LOG.info("handleRequest:content: {}", responseContent); return responseContent; } catch (UnsupportedEncodingException e) { LOG.error("get http content exception: {}", e.toString()); } return null; }
public ContentExchange sendRequest(String url, ContentExchange httpRequest) { httpRequest.setURL(url); try { httpClient.send(httpRequest); } catch (IOException e) { LOG.error("Issues with httpClient.send: {}", e.toString()); } int ex = HttpExchange.STATUS_EXCEPTED; // Waits until the exchange is terminated try { ex = httpRequest.waitForDone(); } catch (InterruptedException e) { LOG.error("Issues with waitForDone: {}", e.toString()); } return httpRequest; }
public OdlOnem2mHttpRequestPrimitiveBuilder setOperationCreate() { onem2mRequest.httpRequest.setMethod("post"); return this; } public OdlOnem2mHttpRequestPrimitiveBuilder setOperationRetrieve() {
@Override protected synchronized void onRetry() throws IOException { if (_fileForUpload != null) { setRequestContent(null); setRequestContentSource(getInputStream()); } else super.onRetry(); }
public OdlOnem2mHttpRequestPrimitiveBuilder setContent(String value) { onem2mRequest.httpRequest.setRequestContentSource(new ByteArrayInputStream(value.getBytes())); onem2mRequest.httpRequest.setRequestContentType("application/json"); return this; }
public OdlOnem2mHttpRequestPrimitive() { httpRequest = new ContentExchange(true); uriQueryString = ""; to = ""; }
public void setURL(String url) { ce.setURL(url); }
HttpFields fields = ex.getResponseFields(); String value = null; value = ex.getResponseContent(); } catch (UnsupportedEncodingException e) { LOG.error("Failed to get response content: {}", e);
public OdlOnem2mHttpRequestPrimitiveBuilder setFrom(String value) { onem2mRequest.httpRequest.setRequestHeader(Onem2m.HttpHeaders.X_M2M_ORIGIN, value); return this; }
public synchronized void setFileForUpload(File fileForUpload) throws IOException { this._fileForUpload = fileForUpload; setRequestContentSource(getInputStream()); } }
@Override public int getResponseStatus() { if ( responseCode > 0 ) { return responseCode; } return super.getResponseStatus(); }
public void addRequestHeader(String key, String s) { ce.addRequestHeader(key, s); }
public void setRequestContent(InputStream ins) { ce.setRequestContentSource(ins); }