@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; }
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; }