List of usage examples for org.apache.http.client HttpResponseException getLocalizedMessage
public String getLocalizedMessage()
From source file:at.ac.tuwien.detlef.gpodder.SyncSubscriptionsAsyncTask.java
@Override public void run() { boolean success = false; /* Retrieve settings. */ GpodderSettings gps = Singletons.i().getGpodderSettings(); DeviceId id = gps.getDeviceId();/* www . ja va 2 s. com*/ if (id == null) { sendError(GENERIC_ERROR, Detlef.getAppContext().getString(R.string.no_gpodder_account_configured)); return; } PodcastDAO pdao = Singletons.i().getPodcastDAO(); String devId = id.toString(); long lastUpdate = gps.getLastUpdate(); MygPodderClient gpc = new MygPodderClient(gps.getUsername(), gps.getPassword(), gps.getApiHostname()); try { EnhancedSubscriptionChanges localChanges = new EnhancedSubscriptionChanges( pdao.getLocallyAddedPodcasts(), pdao.getLocallyDeletedPodcasts(), lastUpdate); UpdateResult result = gpc.updateSubscriptions(devId, localChanges.getAddUrls(), localChanges.getRemoveUrls()); /* Login and get subscription changes */ SubscriptionChanges changes = gpc.pullSubscriptions(devId, lastUpdate); /* Get the Details for the individual URLs. */ PodcastDetailsRetriever pdr = new PodcastDetailsRetriever(); EnhancedSubscriptionChanges remoteChanges = pdr.getPodcastDetails(changes); /* Update the db here */ applySubscriptionChanges(Detlef.getAppContext(), localChanges); applySubscriptionChanges(Detlef.getAppContext(), remoteChanges); /* apply the changed URLs */ if (result.updateUrls != null && result.updateUrls.size() > 0) { for (String oldUrl : result.updateUrls.keySet()) { Podcast p = pdao.getPodcastByUrl(oldUrl); if (p == null) { continue; } String newUrl = result.updateUrls.get(oldUrl); if (newUrl == null) { newUrl = ""; } p.setUrl(newUrl); pdao.update(p); } } /* Update last changed timestamp. */ gps.setLastUpdate(remoteChanges.getTimestamp()); Singletons.i().getGpodderSettingsDAO().writeSettings(gps); success = true; } catch (HttpResponseException e) { String eMsg = e.getLocalizedMessage(); switch (e.getStatusCode()) { case HTTP_STATUS_FORBIDDEN: eMsg = Detlef.getAppContext().getString(R.string.connectiontest_unsuccessful); break; case HTTP_STATUS_NOT_FOUND: eMsg = String.format(Detlef.getAppContext().getString(R.string.device_doesnt_exist_fmt), devId); break; default: break; } sendError(e.getStatusCode(), eMsg); } catch (AuthenticationException e) { sendError(GENERIC_ERROR, e.getLocalizedMessage()); } catch (ClientProtocolException e) { sendError(GENERIC_ERROR, e.getLocalizedMessage()); } catch (IOException e) { sendError(GENERIC_ERROR, e.getLocalizedMessage()); } catch (Exception e) { sendError(GENERIC_ERROR, e.getLocalizedMessage()); } if (!success) { return; } /* Send the result. */ callback.sendEvent(new NoDataResultHandler.NoDataSuccessEvent(callback)); }
From source file:com.xorcode.andtweet.net.ConnectionOAuth.java
private JSONObject postRequest(HttpPost post) throws ConnectionException { JSONObject jso = null;//from ww w. j a v a2 s . c o m boolean ok = false; try { // Maybe we'll need this: // post.setParams(...); // sign the request to authenticate mConsumer.sign(post); String response = mClient.execute(post, new BasicResponseHandler()); jso = new JSONObject(response); ok = true; } catch (HttpResponseException e) { ConnectionException e2 = new ConnectionException(e.getStatusCode(), e.getLocalizedMessage()); Log.w(TAG, e2.getLocalizedMessage()); throw e2; } catch (Exception e) { // We don't catch other exceptions because in fact it's vary difficult to tell // what was a real cause of it. So let's make code clearer. e.printStackTrace(); throw new ConnectionException(e.getLocalizedMessage()); } if (!ok) { jso = null; } return jso; }
From source file:org.tellervo.desktop.wsi.WebJaxbAccessor.java
private INTYPE doRequest() throws IOException { HttpClient client = new ContentEncodingHttpClient(); HttpUriRequest req;/*from w w w.j a v a 2 s . co m*/ JAXBContext context; Document outDocument = null; try { context = getJAXBContext(); } catch (JAXBException jaxb) { throw new IOException("Unable to acquire JAXB context: " + jaxb.getMessage()); } try { if (requestMethod == RequestMethod.POST) { if (this.sendingObject == null) throw new NullPointerException("requestDocument is null yet required for this type of query"); // Create a new POST request HttpPost post = new HttpPost(url); // Make it a multipart post MultipartEntity postEntity = new MultipartEntity(); req = post; // create an XML document from the given objects outDocument = marshallToDocument(context, sendingObject, getNamespacePrefixMapper()); // add it to the http post request XMLBody xmlb = new XMLBody(outDocument, "application/tellervo+xml", null); postEntity.addPart("xmlrequest", xmlb); postEntity.addPart("traceback", new StringBody(getStackTrace())); post.setEntity(postEntity); } else { // well, that's nice and easy req = new HttpGet(url); } // debug this transaction... TransactionDebug.sent(outDocument, noun); // load cookies ((AbstractHttpClient) client).setCookieStore(WSCookieStoreHandler.getCookieStore().toCookieStore()); req.setHeader("User-Agent", "Tellervo WSI " + Build.getUTF8Version() + " (" + clientModuleVersion + "; ts " + Build.getCompleteVersionNumber() + ")"); if (App.prefs.getBooleanPref(PrefKey.WEBSERVICE_USE_STRICT_SECURITY, false)) { // Using strict security so don't allow self signed certificates for SSL } else { // Not using strict security so allow self signed certificates for SSL if (url.getScheme().equals("https")) WebJaxbAccessor.setSelfSignableHTTPSScheme(client); } // create a responsehandler JaxbResponseHandler<INTYPE> responseHandler = new JaxbResponseHandler<INTYPE>(context, receivingObjectClass); // set the schema we validate against responseHandler.setValidateSchema(getValidationSchema()); // execute the actual http query INTYPE inObject = null; try { inObject = client.execute(req, responseHandler); } catch (EOFException e4) { log.debug("Caught EOFException"); } TransactionDebug.received(inObject, noun, context); // save our cookies? WSCookieStoreHandler.getCookieStore().fromCookieStore(((AbstractHttpClient) client).getCookieStore()); // ok, now inspect the document we got back //TellervoDocumentInspector inspector = new TellervoDocumentInspector(inDocument); // Verify our document based on schema validity //inspector.validate(); // Verify our document structure, throw any exceptions! //inspector.verifyDocument(); return inObject; } catch (UnknownHostException e) { throw new IOException("The URL of the server you have specified is unknown"); } catch (HttpResponseException hre) { if (hre.getStatusCode() == 404) { throw new IOException("The URL of the server you have specified is unknown"); } BugReport bugs = new BugReport(hre); bugs.addDocument("sent.xml", outDocument); new BugDialog(bugs); throw new IOException("The server returned a protocol error " + hre.getStatusCode() + ": " + hre.getLocalizedMessage()); } catch (IllegalStateException ex) { throw new IOException("Webservice URL must be a full URL qualified with a communications protocol.\n" + "Tellervo currently supports http:// and https://."); } catch (ResponseProcessingException rspe) { Throwable cause = rspe.getCause(); BugReport bugs = new BugReport(cause); Document invalidDoc = rspe.getNonvalidatingDocument(); File invalidFile = rspe.getInvalidFile(); if (outDocument != null) bugs.addDocument("sent.xml", outDocument); if (invalidDoc != null) bugs.addDocument("recv-nonvalid.xml", invalidDoc); if (invalidFile != null) bugs.addDocument("recv-malformed.xml", invalidFile); new BugDialog(bugs); XMLDebugView.addDocument(BugReport.getStackTrace(cause), "Parsing Exception", true); // it's probably an ioexception... if (cause instanceof IOException) throw (IOException) cause; throw rspe; } catch (XMLParsingException xmlpe) { Throwable cause = xmlpe.getCause(); BugReport bugs = new BugReport(cause); Document invalidDoc = xmlpe.getNonvalidatingDocument(); File invalidFile = xmlpe.getInvalidFile(); bugs.addDocument("sent.xml", outDocument); if (invalidDoc != null) bugs.addDocument("recv-nonvalid.xml", invalidDoc); if (invalidFile != null) bugs.addDocument("recv-malformed.xml", invalidFile); new BugDialog(bugs); XMLDebugView.addDocument(BugReport.getStackTrace(cause), "Parsing Exception", true); // it's probably an ioexception... if (cause instanceof IOException) throw (IOException) cause; throw xmlpe; } catch (IOException ioe) { throw ioe; } catch (Exception uhe) { BugReport bugs = new BugReport(uhe); bugs.addDocument("sent.xml", outDocument); /* // MalformedDocs are handled automatically by BugReport class if(!(uhe instanceof MalformedDocumentException) && inDocument != null) bugs.addDocument("received.xml", inDocument); */ new BugDialog(bugs); throw new IOException("Exception " + uhe.getClass().getName() + ": " + uhe.getLocalizedMessage()); } finally { //? } }