protected String retrieveServerResponse(String text) { Request req = Request.Post(getTagMeServiceUrl().toString()); req.addHeader("Content-Type", "application/x-www-form-urlencoded"); req.bodyForm( Form.form() .add("text", text) .add("gcube-token", getApiKey()) .add("lang", getLanguageCode()) .add("tweet", getIsTweet().toString()) .add("include_abstract", "false") .add("include_categories", "false") .add("include_all_spots", "false") .add("long_text", "0") .add("epsilon", getEpsilon().toString()) .build(), Consts.UTF_8); logger.debug("Request is " + req); Response res = null; try { res = req.execute(); } catch (Exception ex) { throw new GateRuntimeException("Problem executing HTTP request: " + req, ex); } Content cont = null; try { cont = res.returnContent(); } catch (Exception ex) { throw new GateRuntimeException("Problem getting HTTP response content: " + res, ex); } String ret = cont.asString(); logger.debug("TagMe server response " + ret); return ret; }
public static String getByFluent(String uri) throws ClientProtocolException, IOException { Request req = Request.Get(uri); req.addHeader("User-Agent", "curl"); Response res = req.execute(); String back = res.returnContent().asString(); return back; }
/** * Method for http GET * * @param <T> * @param responseType a descendant of CkanResponse * @param path something like /api/3/package_show * @param params list of key, value parameters. They must be not be url encoded. i.e. * "id","laghi-monitorati-trento" * @throws JackanException on error */ <T extends CkanResponse> T getHttp(Class<T> responseType, String path, Object... params) { checkNotNull(responseType); checkNotNull(path); String fullUrl = calcFullUrl(path, params); try { logger.log(Level.FINE, "getting {0}", fullUrl); Request request = Request.Get(fullUrl); if (ckanToken != null) { request.addHeader("Authorization", ckanToken); } if (proxy != null) { request.viaProxy(proxy); } String json = request.execute().returnContent().asString(); T dr = getObjectMapper().readValue(json, responseType); if (!dr.success) { // monkey patching error type throw new JackanException( "Reading from catalog " + catalogURL + " was not successful. Reason: " + CkanError.read(getObjectMapper().readTree(json).get("error").asText())); } return dr; } catch (Exception ex) { throw new JackanException("Error while performing GET. Request url was: " + fullUrl, ex); } }
@When("^\"([^\"]*)\" upload a content without content type$") public void userUploadContentWithoutContentType(String username) throws Throwable { AccessToken accessToken = userStepdefs.tokenByUser.get(username); Request request = Request.Post(uploadUri).bodyByteArray("some text".getBytes(Charsets.UTF_8)); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } response = request.execute().returnResponse(); }
@Test public void test() throws Throwable { Response response = Request.Get("http://localhost:8080/test.json").execute(); System.out.println(response.returnContent().asString()); response = Request.Get("http://localhost:8080/test.html").execute(); System.out.println(response.returnContent().asString()); }
@When("^\"([^\"]*)\" checks for the availability of the upload endpoint$") public void optionUpload(String username) throws Throwable { AccessToken accessToken = userStepdefs.tokenByUser.get(username); Request request = Request.Options(uploadUri); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } response = request.execute().returnResponse(); }
@Then("^\"([^\"]*)\" should be able to retrieve the content$") public void contentShouldBeRetrievable(String username) throws Exception { AccessToken accessToken = userStepdefs.tokenByUser.get(username); Request request = Request.Get(mainStepdefs.baseUri().setPath("/download/" + _1M_ZEROED_FILE_BLOB_ID).build()); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } response = request.execute().returnResponse(); httpAuthorizedStatus(); }
@When("^\"([^\"]*)\" upload a too big content$") public void userUploadTooBigContent(String username) throws Throwable { AccessToken accessToken = userStepdefs.tokenByUser.get(username); Request request = Request.Post(uploadUri) .bodyStream( new BufferedInputStream(new ZeroedInputStream(_10M), _10M), org.apache.http.entity.ContentType.DEFAULT_BINARY); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } response = request.execute().returnResponse(); }
/** * Returns the latest api version supported by the catalog * * @throws JackanException on error */ private synchronized int getApiVersion(int number) { String fullUrl = catalogURL + "/api/" + number; logger.log(Level.FINE, "getting {0}", fullUrl); try { Request request = Request.Get(fullUrl); if (proxy != null) { request.viaProxy(proxy); } String json = request.execute().returnContent().asString(); return getObjectMapper().readValue(json, ApiVersionResponse.class).version; } catch (Exception ex) { throw new JackanException("Error while fetching api version!", this, ex); } }
private void createTmpCollection() throws Exception { Response resp = adminExecutor.execute( Request.Put(dbTmpUri) .bodyString("{a:1}", halCT) .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE)); check("check put tmp db", resp, HttpStatus.SC_CREATED); resp = adminExecutor.execute( Request.Put(collectionTmpUri) .bodyString("{descr:\"temp coll\"}", halCT) .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE)); check("check put tmp coll", resp, HttpStatus.SC_CREATED); }
private static void sendRequest() { // Address API (GET ) try { // Create request Content content = Request.Get( "http://api.matchbox.io/address?addr_1=1600%20Pennsylvania%20Ave&city_state_zip=Washington%20DC%2020500") // Add headers .addHeader( "Cookie", "connect.sid=s%3Apxju73iJEdw8pbqqc9IYk8_HWt3owKfS.fmbhZBZR2ktoIKlAW5GHJPUYH2dMd3tb3KjzOPcRkqA") .addHeader("apikey", "YOUR-API-KEY") // Fetch request and return content .execute() .returnContent(); // Print content System.out.println(content); } catch (IOException e) { System.out.println(e); } }
public List<BpTreeNode> getTree(String id, String ontology, String apiKey) throws IOException { String url = BP_API_BASE + BP_ONTOLOGIES + ontology + "/" + BP_CLASSES + id + "/tree"; HttpResponse response = HttpUtil.makeHttpRequest( Request.Get(url) .addHeader("Authorization", Util.getBioPortalAuthHeader(apiKey)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout)); int statusCode = response.getStatusLine().getStatusCode(); // The tree was successfully retrieved if (statusCode == 200) { ObjectMapper mapper = new ObjectMapper(); JsonNode bpResult = mapper.readTree(new String(EntityUtils.toByteArray(response.getEntity()))); List<BpTreeNode> nodes = new ArrayList<>(); for (JsonNode n : bpResult) { nodes.add(mapper.convertValue(n, BpTreeNode.class)); } return nodes; } else { throw new HTTPException(statusCode); } }
public BpPagedResults<BpClass> findAllClassesInOntology( String ontology, int page, int pageSize, String apiKey) throws HTTPException, IOException { String url = BP_API_BASE + BP_ONTOLOGIES + ontology + "/" + BP_CLASSES + "?include=prefLabel,hasChildren,created,synonym,definition" + "&page=" + page + "&pagesize=" + pageSize; HttpResponse response = HttpUtil.makeHttpRequest( Request.Get(url) .addHeader("Authorization", Util.getBioPortalAuthHeader(apiKey)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout)); int statusCode = response.getStatusLine().getStatusCode(); // The classes were successfully retrieved if (statusCode == 200) { ObjectMapper mapper = new ObjectMapper(); JsonNode bpResult = mapper.readTree(new String(EntityUtils.toByteArray(response.getEntity()))); return mapper.readValue( mapper.treeAsTokens(bpResult), new TypeReference<BpPagedResults<BpClass>>() {}); } else { throw new HTTPException(statusCode); } }
public BpClass find(String id, String ontology, String apiKey) throws HTTPException, IOException { String url = BP_API_BASE + BP_ONTOLOGIES + ontology + "/" + BP_CLASSES + id + "?include=prefLabel,hasChildren,created,synonym,definition"; HttpResponse response = HttpUtil.makeHttpRequest( Request.Get(url) .addHeader("Authorization", Util.getBioPortalAuthHeader(apiKey)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout)); int statusCode = response.getStatusLine().getStatusCode(); // The class was successfully retrieved if (statusCode == 200) { ObjectMapper mapper = new ObjectMapper(); JsonNode bpResult = mapper.readTree(new String(EntityUtils.toByteArray(response.getEntity()))); return mapper.convertValue(bpResult, BpClass.class); } else { throw new HTTPException(statusCode); } }
public BpPagedResults<BpClass> findValueSetsByValueSetCollection( String vsCollection, int page, int pageSize, String apiKey) throws IOException { String url = BP_API_BASE + BP_SEARCH + "?also_search_provisional=true&valueset_roots_only=true&ontology_types=VALUE_SET_COLLECTION&ontologies=" + vsCollection + "&page=" + page + "&pagesize=" + pageSize; HttpResponse response = HttpUtil.makeHttpRequest( Request.Get(url) .addHeader("Authorization", Util.getBioPortalAuthHeader(apiKey)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout)); int statusCode = response.getStatusLine().getStatusCode(); // Success if (statusCode == 200) { ObjectMapper mapper = new ObjectMapper(); JsonNode bpResult = mapper.readTree(new String(EntityUtils.toByteArray(response.getEntity()))); return mapper.readValue( mapper.treeAsTokens(bpResult), new TypeReference<BpPagedResults<BpClass>>() {}); } else { throw new HTTPException(statusCode); } }
private String loginWith(String username, String password) throws IOException { return Request.Post("http://localhost:8080/session") .bodyForm(Form.form().add("username", username).add("password", password).build()) .execute() .returnContent() .asString(); }
public List<BpClass> getParents(String id, String ontology, String apiKey) throws IOException { String url = BP_API_BASE + BP_ONTOLOGIES + ontology + "/" + BP_CLASSES + id + "/parents" + "?include=prefLabel,hasChildren,created,synonym,definition"; HttpResponse response = HttpUtil.makeHttpRequest( Request.Get(url) .addHeader("Authorization", Util.getBioPortalAuthHeader(apiKey)) .connectTimeout(connectTimeout) .socketTimeout(socketTimeout)); int statusCode = response.getStatusLine().getStatusCode(); // Success if (statusCode == 200) { ObjectMapper mapper = new ObjectMapper(); JsonNode bpResult = mapper.readTree(new String(EntityUtils.toByteArray(response.getEntity()))); List<BpClass> children = new ArrayList<>(); for (JsonNode n : bpResult) { children.add(mapper.convertValue(n, BpClass.class)); } return children; } else { throw new HTTPException(statusCode); } }
@When("^\"([^\"]*)\" upload a content$") public void userUploadContent(String username) throws Throwable { AccessToken accessToken = userStepdefs.tokenByUser.get(username); Request request = Request.Post(uploadUri) .bodyStream( new BufferedInputStream(new ZeroedInputStream(_1M), _1M), org.apache.http.entity.ContentType.DEFAULT_BINARY); if (accessToken != null) { request.addHeader("Authorization", accessToken.serialize()); } response = Executor.newInstance(HttpClientBuilder.create().disableAutomaticRetries().build()) .execute(request) .returnResponse(); }
private void whenThePracticeStatusQueueServiceIsCalled() throws IOException { String url = StringUtils.replace(STATUS_URL, "{practiceId}", "1"); url = StringUtils.replace(url, "{username}", username); yatspec.log("STATUS_URL", url); Response response = Request.Get(url).connectTimeout(1000).socketTimeout(1000).execute(); httpResponse = response.returnResponse(); responseContent = EntityUtils.toString(httpResponse.getEntity()); }
/** * Create and POST a request to the Watson service * * @param req the Http Servlet request * @param resp the Http Servlet response * @throws ServletException the servlet exception * @throws IOException Signals that an I/O exception has occurred. */ @Override protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); try { String reqURI = req.getRequestURI(); String endpoint = reqURI.substring(reqURI.lastIndexOf('/') + 1); String url = baseURL + "/v1/" + endpoint; // concatenate query params String queryStr = req.getQueryString(); if (queryStr != null) { url += "?" + queryStr; } URI uri = new URI(url).normalize(); logger.info("posting to " + url); Request newReq = Request.Post(uri); newReq.addHeader("Accept", "application/json"); String metadata = req.getHeader("x-watson-metadata"); if (metadata != null) { metadata += "client-ip:" + req.getRemoteAddr(); newReq.addHeader("x-watson-metadata", metadata); } InputStreamEntity entity = new InputStreamEntity(req.getInputStream()); newReq.bodyString(EntityUtils.toString(entity, "UTF-8"), ContentType.APPLICATION_JSON); Executor executor = this.buildExecutor(uri); Response response = executor.execute(newReq); HttpResponse httpResponse = response.returnResponse(); resp.setStatus(httpResponse.getStatusLine().getStatusCode()); ServletOutputStream servletOutputStream = resp.getOutputStream(); httpResponse.getEntity().writeTo(servletOutputStream); servletOutputStream.flush(); servletOutputStream.close(); logger.info("post done"); } catch (Exception e) { // Log something and return an error message logger.log(Level.SEVERE, "got error: " + e.getMessage(), e); resp.setStatus(HttpStatus.SC_BAD_GATEWAY); } }
/** Query the beaker server to get an available port. */ protected int getPortFromCore() throws IOException { String password = System.getenv("beaker_core_password"); String auth = Base64.encodeBase64String(("beaker:" + password).getBytes("ASCII")); String response = Request.Get("http://127.0.0.1:" + corePort + "/rest/plugin-services/getAvailablePort") .addHeader("Authorization", "Basic " + auth) .execute() .returnContent() .asString(); return Integer.parseInt(response); }
@SuppressWarnings("unchecked") Request populate(Request toPopulate) { for (Map.Entry<String, String> header : headers.entrySet()) { toPopulate.addHeader(header.getKey(), header.getValue()); } if (content == null) return toPopulate; if (contentClass.equals(byte[].class)) { toPopulate.bodyByteArray((byte[]) content); } else if (contentClass.equals(String.class)) { toPopulate.bodyString((String) content, contentType); } else if (Map.class.isAssignableFrom(contentClass)) { List<NameValuePair> formContent = Lists.newArrayList(); for (Map.Entry<String, String> val : ((Map<String, String>) content).entrySet()) { formContent.add(new BasicNameValuePair(val.getKey(), val.getValue())); } toPopulate.bodyForm(formContent); } else if (contentClass.equals(File.class)) { toPopulate.bodyFile((File) content, contentType); } else if (InputStream.class.isAssignableFrom(contentClass)) { toPopulate.bodyStream((InputStream) content); } else { throw new IllegalArgumentException( String.format( "Unknown content class %s. Only byte[], String, Map, File and InputStream are accepted", contentClass)); } return toPopulate; }
@Override protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { if (req.getParameter("text") == null || req.getParameter("voice") == null) { req.getRequestDispatcher("/index.jsp").forward(req, resp); } else { boolean download = false; if (req.getParameter("download") != null && req.getParameter("download").equalsIgnoreCase("true")) { download = true; } req.setCharacterEncoding("UTF-8"); try { String queryStr = req.getQueryString(); String url = baseURL + "/v1/synthesize"; if (queryStr != null) { url += "?" + queryStr; } URI uri = new URI(url).normalize(); Request newReq = Request.Get(uri); newReq.addHeader("Accept", "audio/ogg; codecs=opus"); Executor executor = Executor.newInstance().auth(username, password); Response response = executor.execute(newReq); if (download) { resp.setHeader("content-disposition", "attachment; filename=transcript.ogg"); } ServletOutputStream servletOutputStream = resp.getOutputStream(); response.returnResponse().getEntity().writeTo(servletOutputStream); servletOutputStream.flush(); servletOutputStream.close(); } catch (Exception e) { // Log something and return an error message logger.log(Level.SEVERE, "got error: " + e.getMessage(), e); resp.setStatus(HttpStatus.SC_BAD_GATEWAY); } } }
private void createMetadataAndTestData(String aggregationsMetadata) throws Exception { // get the collection etag String etag = getEtag(collectionTmpUri); // post some data String[] data = new String[] { "{\"name\":\"a\",\"age\":10}", "{\"name\":\"a\",\"age\":20}", "{\"name\":\"a\",\"age\":30}", "{\"name\":\"b\",\"age\":40}", "{\"name\":\"b\",\"age\":50}", "{\"name\":\"b\",\"age\":60}", "{\"obj\":{\"name\":\"x\",\"age\":10}}", "{\"obj\":{\"name\":\"x\",\"age\":20}}", "{\"obj\":{\"name\":\"y\",\"age\":10}}", "{\"obj\":{\"name\":\"y\",\"age\":20}}" }; for (String datum : data) { Response resp = adminExecutor.execute( Request.Post(collectionTmpUri) .bodyString(datum, halCT) .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE)); check("check aggregation create test data", resp, HttpStatus.SC_CREATED); } Response resp = adminExecutor.execute( Request.Patch(collectionTmpUri) .bodyString(aggregationsMetadata, halCT) .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE) .addHeader(Headers.IF_MATCH_STRING, etag)); check("check update collection with aggregations metadata", resp, HttpStatus.SC_OK); }
@Test public void testUnboundVariable() throws Exception { String uri = "avg_ages"; String aggregationsMetadata = "{\"aggrs\": [" + "{" + "\"type\":\"mapReduce\"" + "," + "\"uri\": \"" + uri + "\"," + "\"map\": \"function() { emit(this.name, this.age) }\"" + "," + "\"reduce\":\"function(key, values) { return Array.avg(values) }\"" + "," + "\"query\":{\"name\":{\"_$var\":\"name\"}}" + "}]}"; createTmpCollection(); createMetadataAndTestData(aggregationsMetadata); Response resp; URI aggrUri = buildURI( "/" + dbTmpName + "/" + collectionTmpName + "/" + RequestContext._AGGREGATIONS + "/" + uri); resp = adminExecutor.execute(Request.Get(aggrUri)); HttpResponse httpResp = resp.returnResponse(); assertNotNull(httpResp); HttpEntity entity = httpResp.getEntity(); assertNotNull(entity); StatusLine statusLine = httpResp.getStatusLine(); assertNotNull(statusLine); assertEquals("check status code", HttpStatus.SC_BAD_REQUEST, statusLine.getStatusCode()); assertNotNull("content type not null", entity.getContentType()); assertEquals( "check content type", Representation.HAL_JSON_MEDIA_TYPE, entity.getContentType().getValue()); }
/** * @param <T> * @param responseType a descendant of CkanResponse * @param path something like 1/api/3/action/package_create * @param body the body of the POST * @param the content type, i.e. * @param params list of key, value parameters. They must be not be url encoded. i.e. * "id","laghi-monitorati-trento" * @throws JackanException on error */ <T extends CkanResponse> T postHttp( Class<T> responseType, String path, String body, ContentType contentType, Object... params) { checkNotNull(responseType); checkNotNull(path); checkNotNull(body); checkNotNull(contentType); String fullUrl = calcFullUrl(path, params); try { logger.log(Level.FINE, "posting to url {0}", fullUrl); Request request = Request.Post(fullUrl); if (proxy != null) { request.viaProxy(proxy); } Response response = request.bodyString(body, contentType).addHeader("Authorization", ckanToken).execute(); Content out = response.returnContent(); String json = out.asString(); T dr = getObjectMapper().readValue(json, responseType); if (!dr.success) { // monkey patching error type throw new JackanException( "posting to catalog " + catalogURL + " was not successful. Reason: " + CkanError.read(getObjectMapper().readTree(json).get("error").asText())); } return dr; } catch (Exception ex) { throw new JackanException("Error while performing a POST! Request url is:" + fullUrl, ex); } }
public Object get(String name) throws ClientProtocolException, IOException { String args = "name=" + URLEncoder.encode(name, "ISO-8859-1") + "&session=" + URLEncoder.encode(this.session, "ISO-8859-1"); String valueString = Request.Get(urlBase + "/get?" + args) .addHeader("Authorization", auth) .execute() .returnContent() .asString(); NamespaceBinding binding = mapper.readValue(valueString, NamespaceBinding.class); if (!binding.getDefined()) { throw new RuntimeException("name not defined: " + name); } return binding.getValue(); }
public Object set4(String name, Object value, Boolean unset, Boolean sync) throws ClientProtocolException, IOException { Form form = Form.form().add("name", name).add("sync", sync.toString()).add("session", this.session); if (!unset) { form.add("value", mapper.writeValueAsString(value)); } String reply = Request.Post(urlBase + "/set") .addHeader("Authorization", auth) .bodyForm(form.build()) .execute() .returnContent() .asString(); if (!reply.equals("ok")) { throw new RuntimeException(reply); } return value; }
private String getEtag(URI uri) throws IOException { Response resp = adminExecutor.execute(Request.Get(uri)); HttpResponse httpResp = resp.returnResponse(); assertNotNull(httpResp); HttpEntity entity = httpResp.getEntity(); assertNotNull(entity); StatusLine statusLine = httpResp.getStatusLine(); assertNotNull(statusLine); assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode()); assertNotNull("content type not null", entity.getContentType()); assertEquals( "check content type", Representation.HAL_JSON_MEDIA_TYPE, entity.getContentType().getValue()); String content = EntityUtils.toString(entity); assertNotNull("", content); JsonObject json = null; try { json = JsonObject.readFrom(content); } catch (Throwable t) { fail("parsing received json"); } assertNotNull("check not null json", json); assertNotNull("check not null _etag", json.get("_etag")); assertTrue("check _etag is object", json.get("_etag").isObject()); assertNotNull("check not null _etag.$oid", json.get("_etag").asObject().get("$oid")); assertNotNull( "check _etag.$oid is string", json.get("_etag").asObject().get("$oid").isString()); return json.get("_etag").asObject().get("$oid").asString(); }
private String getUrlContentAsString( URI uri, int connectTimeout, int socketTimeout, Charset charset) throws NbaException { Response response = null; try { response = Request.Get(uri).connectTimeout(connectTimeout).socketTimeout(socketTimeout).execute(); } catch (ClientProtocolException e) { throw new NbaException(ExType.ER_0001, e); } catch (IOException e) { throw new NbaException(ExType.ER_0002, e); } String resp; try { resp = new String(response.returnContent().asBytes(), charset); return resp; } catch (ClientProtocolException e) { throw new NbaException(ExType.ER_0003, e); } catch (IOException e) { throw new NbaException(ExType.ER_0004, e); } }