List of usage examples for org.apache.http.client ClientProtocolException printStackTrace
public void printStackTrace()
From source file:es.eucm.eadventure.tracking.prv.service.TrackingPoster.java
public String openSession() { if (baseURL != null) return baseURL; try {/*from w ww . j a va 2 s.co m*/ HttpClient httpclient = new DefaultHttpClient(); URI uri = URIUtils.createURI("http", serviceURL, -1, "", null, null); HttpPost httppost = new HttpPost(uri); HttpResponse response; response = httpclient.execute(httppost); if (Integer.toString(response.getStatusLine().getStatusCode()).startsWith("2")) { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); StringWriter strWriter = new StringWriter(); char[] buffer = new char[1024]; try { Reader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { strWriter.write(buffer, 0, n); } } finally { instream.close(); } baseURL = strWriter.toString(); } } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } return baseURL; }
From source file:org.megam.deccanplato.provider.box.handler.UserImpl.java
/** * @return/*w w w.java 2s . c om*/ */ private Map<String, String> list() { Map<String, String> outMap = new HashMap<>(); final String BOX_UPLOAD = "/users"; Map<String, String> headerMap = new HashMap<String, String>(); headerMap.put("Authorization", "BoxAuth api_key=" + args.get(API_KEY) + "&auth_token=" + args.get(TOKEN)); TransportTools tools = new TransportTools(BOX_URI + BOX_UPLOAD, null, headerMap); String responseBody = null; TransportResponse response = null; try { response = TransportMachinery.get(tools); responseBody = response.entityToString(); System.out.println("OUTPUT:" + responseBody); } catch (ClientProtocolException ce) { ce.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } outMap.put(OUTPUT, responseBody); return outMap; }
From source file:com.lenovo.h2000.services.LicenseServiceImpl.java
@Override public String upload(String filePath, Map<String, String> headers) throws Exception { _logger.info("calling LicenseServiceImpl.upload function"); Map<String, String> params = new HashMap<String, String>(); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost( HttpConfig.parseBaseUrl(headers) + "license/import" + "?" + TypeUtil.mapToString(params));// new HttpPost("http://localhost:3002/license/import"); String responseBody = ""; DataInputStream in = null;// www . java 2s.co m try { _logger.info("filePath" + filePath); File file = new File(filePath); in = new DataInputStream(new FileInputStream(filePath)); byte[] bufferOut = new byte[(int) file.length()]; int bytes = 0; int i = 0; int len = (int) (1024 > file.length() ? file.length() : 1024); while ((bytes = in.read(bufferOut, i, len)) > 0) { if (bytes < 1024) break; else { len = (int) (file.length() - bytes); i += bytes; } } ByteArrayEntity requestEntity = new ByteArrayEntity(bufferOut); requestEntity.setContentEncoding("UTF-8"); requestEntity.setContentType("application/octet-stream"); httpPost.setEntity(requestEntity); HttpResponse response = httpClient.execute(httpPost); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); HttpEntity responseEntity = response.getEntity(); responseBody = EntityUtils.toString(responseEntity); if (file.isFile() && file.exists()) { file.delete(); } } catch (ClientProtocolException e) { e.printStackTrace(); _logger.error("throwing HttpClientUtil.doPost ClientProtocolException with " + e.getMessage()); } catch (IOException e) { e.printStackTrace(); _logger.error("throwing HttpClientUtil.doPost IOException with " + e.getMessage()); } finally { httpClient.close(); if (in != null) { in.close(); } } return responseBody; }
From source file:de.unistuttgart.ipvs.pmp.apps.vhike.tools.JSonRequestReader.java
/** * Try to login into the app with given username and password * //from ww w.j a va2 s. com * @param name * username * @param pw * password * @return status (see vHike/webservice/design.html) */ public static String login(String username, String pw) { listToParse.clear(); listToParse.add(new ParamObject("username", username, true)); listToParse.add(new ParamObject("password", pw, true)); JsonObject object = null; try { object = JSonRequestProvider.doRequest(listToParse, "login.php"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } boolean suc = false; if (object != null) { suc = object.get("successful").getAsBoolean(); } String sid = null; String status = null; if (suc) { status = object.get("status").getAsString(); Log.d(TAG, "STATUS NACH DEM LOGIN:" + status); if (!status.equals("invalid")) { sid = object.get("sid").getAsString(); Model.getInstance().setSid(sid); Model.getInstance().setOwnProfile(getOwnProfile(sid)); } return status; } return status; }
From source file:org.cgiar.ccafs.ap.util.ClientRepository.java
public Element getXmlRestClient(String linkRequest) { DefaultHttpClient httpClient = new DefaultHttpClient(); httpClient = this.verifiedClient(httpClient); HttpGet getRequest = new HttpGet(linkRequest); getRequest.addHeader("accept", "application/xml"); HttpResponse response = null;/*from www. j a v a 2 s.com*/ try { response = httpClient.execute(getRequest); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } InputStreamReader br; try { br = new InputStreamReader((response.getEntity().getContent())); Element metadata = getDocumentRoot(br); return metadata; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:de.unistuttgart.ipvs.pmp.apps.vhike.tools.JSonRequestReaderWS.java
/** * Driver search for the potential hitchhiker. * //from ww w . j a v a 2 s.c o m * @param sid * @param lat * @param lon * @param perimeter * /radius */ public static List<QueryObject> searchQuery(String sid, float lat, float lon, int perimeter) { listToParse.clear(); listToParse.add(new ParamObject("sid", sid, false)); listToParse.add(new ParamObject("lat", String.valueOf(lat), true)); listToParse.add(new ParamObject("lon", String.valueOf(lon), true)); listToParse.add(new ParamObject("distance", String.valueOf(perimeter), true)); JsonObject object = null; try { object = JSonRequestProvider.doRequest(listToParse, "query_search.php"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } boolean suc = false; List<QueryObject> queryObjects = null; if (object != null) { suc = object.get("successful").getAsBoolean(); if (suc) { String status = object.get("status").getAsString(); if (status.equals("result")) { JsonArray array = object.get("queries").getAsJsonArray(); Log.i(TAG, array.toString()); queryObjects = new ArrayList<QueryObject>(); /* * "queryid": Integer, "userid": Integer, "username": * String, "rating": Float, "lat": Float, "lon": Float, * "seats": Integer, distance: Float */ for (int i = 0; i < array.size(); i++) { JsonObject Iobject = array.get(i).getAsJsonObject(); int queryid = Iobject.get("queryid").getAsInt(); int userid = Iobject.get("userid").getAsInt(); String username = Iobject.get("username").getAsString(); // float rating = Iobject.get("rating").getAsFloat(); float cur_lat = Iobject.get("lat").getAsFloat(); float cur_lon = Iobject.get("lon").getAsFloat(); int seats = Iobject.get("seats").getAsInt(); float distance = Iobject.get("distance").getAsFloat(); QueryObject qObject = new QueryObject(queryid, userid, username, cur_lat, cur_lon, seats, distance); queryObjects.add(qObject); } if (queryObjects != null) { Model.getInstance().setQueryHolder(queryObjects); } return queryObjects; } } } if (queryObjects != null) { Model.getInstance().setQueryHolder(queryObjects); } return queryObjects; }
From source file:com.sgcv.rest.jsf.web.cliente.bbean.ProductoBBean.java
public List<Producto> getDocuments() throws Exception { if (documents != null) { return documents; }/* w w w. j a v a 2 s .c om*/ /*//Envia el path de peticion get de producto, listado de producto ClientResponse response = restClient.clientGetResponse("producto"); //verifica que no haya error con la pagina.. si es 200 caso de exito, sino fallo if (response.getStatus() != 200) { throw new RuntimeException("Failed service call: HTTP error code : " + response.getStatus()); }*/ try { ClientRequest request = new ClientRequest( "http://localhost:8080/Rest-JSF-Web-PrimeFaces/webresources/producto"); request.accept("application/json"); ClientResponse<String> response = request.get(String.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); } // get producto as JSON, el json se guarda como string jsonHistory = response.getEntity(String.class); // convert to Java array / list of Cliente instances, Convierte el json a una Lista de Productos //fromJson pasa de la notacion json a java, Si fuera en caso contrario se usaria toJson Producto[] docs = GsonConverter.getGson().fromJson(jsonHistory, Producto[].class); documents = Arrays.asList(docs); return documents; } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:de.unistuttgart.ipvs.pmp.apps.vhike.tools.JSonRequestReader.java
/** * Start searching for drivers/* www . j ava 2 s . co m*/ * * @param sid * @param destination * @param current_lat * @param current_lon * @param seats * @return ID of the query */ public static int startQuery(String sid, String destination, float current_lat, float current_lon, int seats) { Log.d(TAG, sid); Log.d(TAG, destination); Log.d(TAG, String.valueOf(current_lat)); Log.d(TAG, String.valueOf(current_lon)); Log.d(TAG, String.valueOf(seats)); listToParse.clear(); listToParse.add(new ParamObject("sid", sid, false)); listToParse.add(new ParamObject("destination", destination, true)); listToParse.add(new ParamObject("current_lat", String.valueOf(current_lat), true)); listToParse.add(new ParamObject("current_lon", String.valueOf(current_lon), true)); listToParse.add(new ParamObject("seats", String.valueOf(seats), true)); JsonObject object = null; try { object = JSonRequestProvider.doRequest(listToParse, "query_start.php"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } boolean suc = false; int id = Constants.QUERY_ID_ERROR; if (object != null) { suc = object.get("successful").getAsBoolean(); if (suc) { id = object.get("id").getAsInt(); Log.d(TAG, String.valueOf(id)); return id; } } return id; }
From source file:com.ibm.dtf.EmployeeAdapterResource.java
@ApiOperation(value = "Get employee list", notes = "Return employee list") @ApiResponses(value = { @ApiResponse(code = 200, message = "A constant string is returned") }) @GET/*ww w. j a v a2s .co m*/ @Path("/list") @Produces(MediaType.TEXT_PLAIN) public String employees() { System.out.println(">> in employees() ..."); logger.info(">> EmployeeAdapterResource: employees"); String rsp = null; try { rsp = getHttp(API_ENDPOINT); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rsp; }
From source file:de.unistuttgart.ipvs.pmp.apps.vhike.tools.JSonRequestReader.java
/** * This method send a request to the web service to register an user in the database * /* www.j av a 2 s .com*/ * @param username * @param password * @param email * @param firstname * @param lastname * @param tel * @param description * @param email_public * @param firstname_public * @param lastname_public * @param tel_public * @return status (see vHike/webservice/design.html) */ public static String register(String username, String password, String email, String firstname, String lastname, String tel, String description, boolean email_public, boolean firstname_public, boolean lastname_public, boolean tel_public) { listToParse.clear(); listToParse.add(new ParamObject("username", username, true)); listToParse.add(new ParamObject("password", password, true)); listToParse.add(new ParamObject("email", email, true)); listToParse.add(new ParamObject("firstname", firstname, true)); listToParse.add(new ParamObject("lastname", lastname, true)); listToParse.add(new ParamObject("tel", tel, true)); listToParse.add(new ParamObject("email_public", String.valueOf(email_public), true)); listToParse.add(new ParamObject("firstname_public", String.valueOf(firstname_public), true)); listToParse.add(new ParamObject("lastname_public", String.valueOf(lastname_public), true)); listToParse.add(new ParamObject("tel_public", String.valueOf(tel_public), true)); JsonObject object = null; try { object = JSonRequestProvider.doRequest(listToParse, "register.php"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } String status = ""; if (object != null) { boolean suc = object.get("successful").getAsBoolean(); if (suc) { status = object.get("status").getAsString(); } return status; } else { return status; } }