List of usage examples for java.net MalformedURLException toString
public String toString()
From source file:com.denimgroup.threadfix.service.defects.utils.RestUtilsImpl.java
@Nonnull private InputStream postUrl(String urlString, String data, String username, String password, String contentType) {//from w w w. ja v a 2 s .c o m URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { LOG.warn("URL used for POST was bad: '" + urlString + "'"); throw new RestUrlException(e, "Received a malformed server URL."); } HttpURLConnection httpConnection = null; OutputStreamWriter outputWriter = null; try { if (proxyService == null) { httpConnection = (HttpURLConnection) url.openConnection(); } else { httpConnection = proxyService.getConnectionWithProxyConfig(url, classToProxy); } setupAuthorization(httpConnection, username, password); httpConnection.addRequestProperty("Content-Type", contentType); httpConnection.addRequestProperty("Accept", contentType); httpConnection.setDoOutput(true); outputWriter = new OutputStreamWriter(httpConnection.getOutputStream()); outputWriter.write(data); outputWriter.flush(); InputStream is = httpConnection.getInputStream(); return is; } catch (IOException e) { LOG.warn("IOException encountered trying to post to URL with message: " + e.getMessage()); if (httpConnection == null) { LOG.warn( "HTTP connection was null so we cannot do further debugging of why the HTTP request failed"); } else { try { InputStream errorStream = httpConnection.getErrorStream(); if (errorStream == null) { LOG.warn("Error stream from HTTP connection was null"); } else { LOG.warn( "Error stream from HTTP connection was not null. Attempting to get response text."); setPostErrorResponse(IOUtils.toString(errorStream)); LOG.warn("Error text in response was '" + getPostErrorResponse() + "'"); throw new RestIOException(e, getPostErrorResponse(), "Unable to get response from server. Error text was: " + getPostErrorResponse(), getStatusCode(httpConnection)); } } catch (IOException e2) { LOG.warn("IOException encountered trying to read the reason for the previous IOException: " + e2.getMessage(), e2); throw new RestIOException(e2, "Unable to read response from server." + e2.getMessage(), getStatusCode(httpConnection)); } } throw new RestIOException(e, "Unable to read response from server." + e.toString()); } finally { if (outputWriter != null) { try { outputWriter.close(); } catch (IOException e) { LOG.warn("Failed to close output stream in postUrl.", e); } } } }
From source file:org.cytoscape.app.internal.manager.SimpleApp.java
@Override public Object createAppInstance(CySwingAppAdapter appAdapter) throws AppInstanceException { File installFile = this.getAppTemporaryInstallFile(); if (installFile == null) { throw new AppInstanceException("No copy of app jar for instancing was found"); }//from w w w . j ava 2s . c o m URL appURL = null; try { appURL = installFile.toURI().toURL(); } catch (MalformedURLException e) { throw new AppInstanceException( "Unable to obtain URL for file: " + installFile + ". Reason: " + e.getMessage()); } // TODO: Currently uses the CyAppAdapter's loader to load apps' classes. Is there reason to use a different one? ClassLoader appClassLoader = new URLClassLoader(new URL[] { appURL }, appAdapter.getClass().getClassLoader()); // Attempt to load the class Class<?> appEntryClass = null; try { appEntryClass = appClassLoader.loadClass(this.getEntryClassName()); } catch (ClassNotFoundException e) { throw new AppInstanceException("Class " + this.getEntryClassName() + " not found in URL: " + appURL); } // Attempt to obtain the constructor Constructor<?> constructor = null; try { try { constructor = appEntryClass.getConstructor(CyAppAdapter.class); } catch (SecurityException e) { throw new AppInstanceException("Access to the constructor for " + appEntryClass + " denied."); } catch (NoSuchMethodException e) { throw new AppInstanceException("Unable to find a constructor for " + appEntryClass + " that takes a CyAppAdapter as its argument."); } } catch (AppInstanceException e) { try { constructor = appEntryClass.getConstructor(CySwingAppAdapter.class); } catch (SecurityException e2) { throw new AppInstanceException("Access to the constructor for " + appEntryClass + " taking a CySwingAppAdapter as its argument denied."); } catch (NoSuchMethodException e2) { throw new AppInstanceException("Unable to find an accessible constructor that takes either" + " a CyAppAdapter or a CySwingAppAdapter as its argument."); } } // Attempt to instantiate the app's class that extends AbstractCyApp or AbstractCySwingApp. Object appInstance = null; try { appInstance = constructor.newInstance(appAdapter); } catch (IllegalArgumentException e) { throw new AppInstanceException( "Illegal arguments passed to the constructor for the app's entry class: " + e.getMessage()); } catch (InstantiationException e) { throw new AppInstanceException( "Error instantiating the class " + appEntryClass + ": " + e.getMessage()); } catch (IllegalAccessException e) { throw new AppInstanceException("Access to constructor denied: " + e.getMessage()); } catch (InvocationTargetException e) { e.printStackTrace(); throw new AppInstanceException("App constructor threw exception: " + e.toString()); } return appInstance; }
From source file:typoscript.TypoScriptPluginOptions.java
/** * Checks to ensure all fields are filled out and that login credentials check out * //from w ww .j a v a 2 s . co m * @param reportOK Whether to report on success (false if this is a passive check before adding) * @return true if everything checks out */ private boolean test(boolean reportOK) { URL urlFull = null; URL urlBase = null; if (txtURL.getText().length() == 0) { JOptionPane.showMessageDialog(this, "You must specify a URL", "URL Field Blank", JOptionPane.ERROR_MESSAGE); return false; } else { try { String typeNum = jEdit.getProperty(TypoScriptPlugin.PROPERTY_PREFIX + "typenum"); urlFull = new URL(txtURL.getText() + "index.php?type=" + typeNum); urlBase = new URL(txtURL.getText()); } catch (MalformedURLException e) { JOptionPane.showMessageDialog(this, "The URL you entered was not valid.\nPlease ensure it starts with http:// or https:// and contains no spaces or other characters that would normally be disallowed in a URL", "Invalid URL", JOptionPane.ERROR_MESSAGE); return false; } if (!txtURL.getText().endsWith("/")) { JOptionPane.showMessageDialog(this, "The URL you entered did not end with a '/' character. This is required.\nIf you would normally login at http://typo3.example.com/typo3/, then you need to enter http://typo3.example.com/", "Invalid URL", JOptionPane.ERROR_MESSAGE); return false; } } if (txtUser.getText().length() == 0) { JOptionPane.showMessageDialog(this, "You must specify a username, even if you have some alternative authentication scheme (make one up)", "Username Field Blank", JOptionPane.ERROR_MESSAGE); return false; } if (txtPass.getPassword().length == 0) { JOptionPane.showMessageDialog(this, "You must specify a password", "Password Field Blank", JOptionPane.ERROR_MESSAGE); return false; } // First stage checks out. Disable the buttons and check the provided information disableButtons(); this.pack(); // resize window try { HttpClient client = new HttpClient(); HeadMethod head = new HeadMethod(urlFull.toString()); client.executeMethod(head); //head.execute() if (head.getStatusCode() != HttpStatus.SC_OK) { JOptionPane.showMessageDialog(this, "Received a non-200 response code from the server: " + head.getStatusCode() + "(" + head.getStatusText() + ")\nPlease ensure there are no rewrite rules or redirects in the way.\nI tried URL: " + urlFull.toString(), "Non-200 response", JOptionPane.ERROR_MESSAGE); resetButtonState(); return false; } if (head.getResponseHeader("X-jeditvfs-present") == null) { JOptionPane.showMessageDialog(this, "The jEdit VFS extension doesn't appear to be installed on the remote site.\nPlease install the 'jeditvfs' plugin from the TYPO3 extension repository and try again.\nIt's also possible the URL didn't point to the right place.\nYou shoud be pointing to the root of the FRONTEND of your site.\nI tried: " + urlFull.toString(), "jeditvfs extension not located", JOptionPane.ERROR_MESSAGE); resetButtonState(); return false; } } catch (IOException e) { JOptionPane.showMessageDialog(this, "An I/O error occured while testing the site.\nPlease check your internet connection. Details below:\n" + e.toString(), "IOException", JOptionPane.ERROR_MESSAGE); resetButtonState(); return false; } // That worked. Now we need to construct a real T3Site object and perform an authentication check (and get the site name in the process) curSite = new T3Site(urlBase, urlFull, txtUser.getText(), new String(txtPass.getPassword()), chkClearCache.isSelected()); try { curSite.setName(curSite.getWorker().getSiteTitle()); } catch (IOException e) { JOptionPane.showMessageDialog(this, "An I/O error occured while testing the site.\nPlease check your internet connection. Details below:\n" + e.toString(), "IOException", JOptionPane.ERROR_MESSAGE); resetButtonState(); return false; } catch (XmlRpcException e) { // Probably an authentication failure. if (e.code == RemoteCallWorker.JEDITVFS_ERROR_AUTHFAIL) { JOptionPane.showMessageDialog(this, "Authentication failed.\nPlease make sure you typed your username and password correctly.\nNote also that you require an admin-level backend account to use this system.\nIf you get this failure and are using an exotic authentication scheme, please contact the developer of this plugin to report a bug.\nServer said: " + e.getMessage(), "Backend authentication failed", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog(this, "The server returned an unexpected response in reply to an authentication test\nServer said: " + e.getMessage(), "Unexpected XML-RPC exception", JOptionPane.ERROR_MESSAGE); } resetButtonState(); return false; } if (reportOK) { JOptionPane.showMessageDialog(this, "All OK", "All tests passed", JOptionPane.INFORMATION_MESSAGE); } resetButtonState(); return true; }
From source file:com.jimplush.goose.ContentExtractor.java
public Article performExtraction(String urlToCrawl, String rawHtml) { urlToCrawl = getUrlToCrawl(urlToCrawl); try {/*from ww w.j a va 2 s . c om*/ new URL(urlToCrawl); this.linkhash = HashUtils.md5(urlToCrawl); } catch (MalformedURLException e) { throw new IllegalArgumentException("Invalid URL Passed in: " + urlToCrawl, e); } ParseWrapper parseWrapper = new ParseWrapper(); Article article = null; try { if (rawHtml == null) { rawHtml = HtmlFetcher.getHtml(urlToCrawl); } article = new Article(); article.setRawHtml(rawHtml); Document doc = parseWrapper.parse(rawHtml, urlToCrawl); // before we cleanse, provide consumers with an opportunity to extract the publish date article.setPublishDate(config.getPublishDateExtractor().extract(doc)); // now allow for any additional data to be extracted article.setAdditionalData(config.getAdditionalDataExtractor().extract(doc)); // grab the text nodes of any <a ... rel="tag">Tag Name</a> elements article.setTags(extractTags(doc)); // now perform a nice deep cleansing DocumentCleaner documentCleaner = getDocCleaner(); doc = documentCleaner.clean(doc); article.setTitle(getTitle(doc)); article.setMetaDescription(getMetaDescription(doc)); article.setMetaKeywords(getMetaKeywords(doc)); article.setCanonicalLink(getCanonicalLink(doc, urlToCrawl)); article.setDomain(article.getCanonicalLink()); // extract the content of the article article.setTopNode(calculateBestNodeBasedOnClustering(doc)); if (article.getTopNode() != null) { // extract any movie embeds out from our main article content article.setMovies(extractVideos(article.getTopNode())); if (config.isEnableImageFetching()) { HttpClient httpClient = HtmlFetcher.getHttpClient(); imageExtractor = getImageExtractor(httpClient, urlToCrawl); article.setTopImage(imageExtractor.getBestImage(doc, article.getTopNode())); } // grab siblings and remove high link density elements cleanupNode(article.getTopNode()); outputFormatter = getOutputFormatter(); article.setCleanedArticleText(outputFormatter.getFormattedText(article.getTopNode())); if (logger.isDebugEnabled()) { logger.debug("FINAL EXTRACTION TEXT: \n" + article.getCleanedArticleText()); } if (config.isEnableImageFetching()) { if (logger.isDebugEnabled()) { logger.debug("\n\nFINAL EXTRACTION IMAGE: \n" + article.getTopImage().getImageSrc()); } } } // cleans up all the temp images that we've downloaded releaseResources(); } catch (MaxBytesException e) { logger.error(e.toString(), e); } catch (NotHtmlException e) { logger.error("URL: " + urlToCrawl + " did not contain valid HTML to parse, exiting. " + e.toString()); } catch (Exception e) { logger.error("General Exception occured on url: " + urlToCrawl + " " + e.toString()); // throw new RuntimeException(e); } return article; }
From source file:org.opendatakit.survey.android.tasks.DownloadFormsTask.java
/** * Common routine to download a document from the downloadUrl and save the * contents in the file 'f'. Shared by media file download and form file * download.//from www .j a va 2s . c o m * * @param f * @param downloadUrl * @throws Exception */ private void downloadFile(File f, String downloadUrl) throws Exception { URI uri = null; try { // assume the downloadUrl is escaped properly URL url = new URL(downloadUrl); uri = url.toURI(); } catch (MalformedURLException e) { WebLogger.getLogger(appName).printStackTrace(e); throw e; } catch (URISyntaxException e) { WebLogger.getLogger(appName).printStackTrace(e); throw e; } // WiFi network connections can be renegotiated during a large form // download sequence. // This will cause intermittent download failures. Silently retry once // after each // failure. Only if there are two consecutive failures, do we abort. boolean success = false; int attemptCount = 1; while (!success && attemptCount++ <= 2) { if (isCancelled()) { throw new Exception("cancelled"); } // get shared HttpContext so that authentication and cookies are // retained. HttpContext localContext = ClientConnectionManagerFactory.get(appName).getHttpContext(); HttpClient httpclient = ClientConnectionManagerFactory.get(appName) .createHttpClient(WebUtils.CONNECTION_TIMEOUT); // set up request... HttpGet req = WebUtils.get().createOpenRosaHttpGet(uri, mAuth); HttpResponse response = null; try { response = httpclient.execute(req, localContext); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != 200) { WebUtils.get().discardEntityBytes(response); String errMsg = appContext.getString(R.string.file_fetch_failed, downloadUrl, response.getStatusLine().getReasonPhrase(), statusCode); WebLogger.getLogger(appName).e(t, errMsg); throw new Exception(errMsg); } // write connection to file InputStream is = null; OutputStream os = null; try { is = response.getEntity().getContent(); os = new FileOutputStream(f); byte buf[] = new byte[1024]; int len; while ((len = is.read(buf)) > 0) { os.write(buf, 0, len); } os.flush(); success = true; } finally { if (os != null) { try { os.close(); } catch (Exception e) { } } if (is != null) { try { // ensure stream is consumed... final long count = 1024L; while (is.skip(count) == count) ; } catch (Exception e) { // no-op } try { is.close(); } catch (Exception e) { } } } } catch (Exception e) { ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); WebLogger.getLogger(appName).e(t, e.toString()); WebLogger.getLogger(appName).printStackTrace(e); if (attemptCount != 1) { throw e; } } } }
From source file:org.opendatakit.services.legacy.tasks.InstanceUploaderTask.java
@Override protected InstanceUploadOutcome doInBackground(String... toUpload) { mOutcome = new InstanceUploadOutcome(); mOutcome.mResults = new HashMap<String, String>(); mOutcome.mAuthRequestingServer = null; PropertiesSingleton props = CommonToolProperties.get(appContext, appName); String urlString = null;//from w ww . java 2 s . co m urlString = props.getProperty(CommonToolProperties.KEY_SYNC_SERVER_URL); urlString = urlString + "/submission"; URI u = null; try { URL url = new URL(URLDecoder.decode(urlString, CharEncoding.UTF_8)); u = url.toURI(); } catch (MalformedURLException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put("unknown", fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); return mOutcome; } catch (URISyntaxException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put("unknown", fail + "invalid uri: " + urlString + " :: details: " + e.getMessage()); return mOutcome; } catch (UnsupportedEncodingException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put("unknown", fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); return mOutcome; } CredentialsProvider credsProvider = new BasicCredentialsProvider(); CookieStore cookieStore = new BasicCookieStore(); String authType = props.getProperty(CommonToolProperties.KEY_AUTHENTICATION_TYPE); if (authType.equals(appContext.getString(R.string.credential_type_username_password))) { String username = props.getProperty(CommonToolProperties.KEY_USERNAME); String password = props.getProperty(CommonToolProperties.KEY_PASSWORD); registerUsernamePassword(credsProvider, username, password, u.getHost()); } else if (authType.equals(appContext.getString(R.string.credential_type_google_account))) { throw new IllegalStateException("legacy doesn't support Google Authentication"); } // context holds authentication state machine, so it cannot be // shared across independent activities. HttpClientContext localContext = HttpClientContext.create(); localContext.setCookieStore(cookieStore); localContext.setCredentialsProvider(credsProvider); CloseableHttpClient httpclient = WebUtils.createHttpClient(WebUtils.CONNECTION_TIMEOUT); Map<URI, URI> uriRemap = new HashMap<URI, URI>(); for (int i = 0; i < toUpload.length; ++i) { if (isCancelled()) { return mOutcome; } publishProgress(i + 1, toUpload.length); Uri toUpdate = Uri.withAppendedPath(InstanceProviderAPI.CONTENT_URI, appName + "/" + uploadTableId + "/" + StringEscapeUtils.escapeHtml4(toUpload[i])); Cursor c = null; try { c = appContext.getContentResolver().query(toUpdate, null, null, null, null); if (c.getCount() == 1 && c.moveToFirst()) { String id = CursorUtils.getIndexAsString(c, c.getColumnIndex(InstanceColumns._ID)); String dataTableInstanceId = CursorUtils.getIndexAsString(c, c.getColumnIndex(InstanceColumns.DATA_INSTANCE_ID)); String lastOutcome = CursorUtils.getIndexAsString(c, c.getColumnIndex(InstanceColumns.XML_PUBLISH_STATUS)); String submissionInstanceId = LocalizationUtils.genUUID(); // submissions always get a new legacy instance id UNLESS the last // submission failed, // in which case we retry the submission using the legacy instance id // associated with // that failure. This supports resumption of sends of forms with many // attachments. if (lastOutcome != null && lastOutcome.equals(InstanceColumns.STATUS_SUBMISSION_FAILED)) { String lastId = CursorUtils.getIndexAsString(c, c.getColumnIndex(InstanceColumns.SUBMISSION_INSTANCE_ID)); if (lastId != null) { submissionInstanceId = lastId; } } c.close(); FileSet instanceFiles; try { instanceFiles = constructSubmissionFiles(dataTableInstanceId, submissionInstanceId); // NOTE: /submission must not be translated! It is // the well-known path on the server. if (!uploadOneSubmission(urlString, toUpdate, id, submissionInstanceId, instanceFiles, httpclient, localContext, uriRemap)) { return mOutcome; // get credentials... } } catch (JsonParseException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId + " :: details: " + e.toString()); } catch (JsonMappingException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId + " :: details: " + e.toString()); } catch (IOException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "unable to obtain manifest: " + dataTableInstanceId + " :: details: " + e.toString()); } } else { mOutcome.mResults.put("unknown", fail + "unable to retrieve instance information via: " + toUpdate.toString()); } } finally { if (c != null && !c.isClosed()) { c.close(); } } } return mOutcome; }
From source file:org.opendatakit.services.legacy.tasks.InstanceUploaderTask.java
/** * Uploads to urlString the submission identified by id with filepath of * instance/*from ww w . ja v a2 s. co m*/ * * @param urlString * destination URL * @param toUpdate * @param id * -- _ID in the InstanceColumns table. * @param submissionInstanceId * @param instanceFiles * @param httpclient * - client connection * @param localContext * - context (e.g., credentials, cookies) for client connection * @param uriRemap * - mapping of Uris to avoid redirects on subsequent invocations * @return false if credentials are required and we should terminate * immediately. */ private boolean uploadOneSubmission(String urlString, Uri toUpdate, String id, String submissionInstanceId, FileSet instanceFiles, HttpClient httpclient, HttpContext localContext, Map<URI, URI> uriRemap) { ContentValues cv = new ContentValues(); cv.put(InstanceColumns.SUBMISSION_INSTANCE_ID, submissionInstanceId); URI u = null; try { URL url = new URL(URLDecoder.decode(urlString, CharEncoding.UTF_8)); u = url.toURI(); } catch (MalformedURLException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (URISyntaxException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid uri: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnsupportedEncodingException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } // NOTE: ODK Survey assumes you are interfacing with an // OpenRosa-compliant server if (uriRemap.containsKey(u)) { // we already issued a head request and got a response, // so we know the proper URL to send the submission to // and the proper scheme. We also know that it was an // OpenRosa compliant server. u = uriRemap.get(u); } else { // we need to issue a head request HttpHead httpHead = WebUtils.get().createOpenRosaHttpHead(u); // prepare response HttpResponse response = null; try { response = httpclient.execute(httpHead, localContext); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 401) { WebUtils.get().discardEntityBytes(response); // we need authentication, so stop and return what we've // done so far. mOutcome.mAuthRequestingServer = u; return false; } else if (statusCode == 204) { Header[] locations = response.getHeaders("Location"); WebUtils.get().discardEntityBytes(response); if (locations != null && locations.length == 1) { try { URL url = new URL(URLDecoder.decode(locations[0].getValue(), CharEncoding.UTF_8)); URI uNew = url.toURI(); if (u.getHost().equalsIgnoreCase(uNew.getHost())) { // trust the server to tell us a new location // ... and possibly to use https instead. uriRemap.put(u, uNew); u = uNew; } else { // Don't follow a redirection attempt to a // different host. // We can't tell if this is a spoof or not. mOutcome.mResults.put(id, fail + "Unexpected redirection attempt to a different host: " + uNew.toString()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + urlString + " " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } else { // may be a server that does not handle WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).w(t, "Status code on Head request: " + statusCode); if (statusCode >= 200 && statusCode <= 299) { mOutcome.mResults.put(id, fail + "Invalid status code on Head request. If you have a web proxy, you may need to login to your network. "); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } catch (ClientProtocolException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); mOutcome.mResults.put(id, fail + "Client Protocol Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (ConnectTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnknownHostException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + e.getMessage() + " :: Network Connection Failed"); WebLogger.getLogger(appName).e(t, e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (SocketTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (HttpHostConnectException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.toString()); mOutcome.mResults.put(id, fail + "Network Connection Refused"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "Generic Exception"); WebLogger.getLogger(appName).e(t, e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // At this point, we may have updated the uri to use https. // This occurs only if the Location header keeps the host name // the same. If it specifies a different host name, we error // out. // // And we may have set authentication cookies in our // cookiestore (referenced by localContext) that will enable // authenticated publication to the server. // // get instance file File instanceFile = instanceFiles.instanceFile; if (!instanceFile.exists()) { mOutcome.mResults.put(id, fail + "instance XML file does not exist!"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } List<MimeFile> files = instanceFiles.attachmentFiles; boolean first = true; int j = 0; int lastJ; while (j < files.size() || first) { lastJ = j; first = false; HttpPost httppost = WebUtils.get().createOpenRosaHttpPost(u); long byteCount = 0L; // mime post MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // add the submission file first... builder.addBinaryBody("xml_submission_file", instanceFile, ContentType.TEXT_XML.withCharset(Charset.forName(CharEncoding.UTF_8)), instanceFile.getName()); WebLogger.getLogger(appName).i(t, "added xml_submission_file: " + instanceFile.getName()); byteCount += instanceFile.length(); for (; j < files.size(); j++) { MimeFile mf = files.get(j); File f = mf.file; String contentType = mf.contentType; builder.addBinaryBody(f.getName(), f, ContentType.create(contentType), f.getName()); byteCount += f.length(); WebLogger.getLogger(appName).i(t, "added " + contentType + " file " + f.getName()); // we've added at least one attachment to the request... if (j + 1 < files.size()) { long nextFileLength = (files.get(j + 1).file.length()); if ((j - lastJ + 1 > 100) || (byteCount + nextFileLength > 10000000L)) { // the next file would exceed the 10MB threshold... WebLogger.getLogger(appName).i(t, "Extremely long post is being split into multiple posts"); try { builder.addTextBody("*isIncomplete*", "yes", ContentType.TEXT_PLAIN.withCharset(Charset.forName((CharEncoding.UTF_8)))); } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); // never happens... } ++j; // advance over the last attachment added... break; } } } httppost.setEntity(builder.build()); // prepare response and return uploaded HttpResponse response = null; try { response = httpclient.execute(httppost, localContext); int responseCode = response.getStatusLine().getStatusCode(); WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).i(t, "Response code:" + responseCode); // verify that the response was a 201 or 202. // If it wasn't, the submission has failed. if (responseCode != 201 && responseCode != 202) { if (responseCode == 200) { mOutcome.mResults.put(id, fail + "Network login failure? Again?"); } else { mOutcome.mResults.put(id, fail + response.getStatusLine().getReasonPhrase() + " (" + responseCode + ") at " + urlString); } cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "Generic Exception. " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // if it got here, it must have worked mOutcome.mResults.put(id, appContext.getString(R.string.success)); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMITTED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; }
From source file:org.opendatakit.survey.android.tasks.InstanceUploaderTask.java
/** * Uploads to urlString the submission identified by id with filepath of * instance//from w w w . j a v a 2 s . c o m * * @param urlString * destination URL * @param id * -- _ID in the InstanceColumns table. * @param instanceFilePath * @param httpclient * - client connection * @param localContext * - context (e.g., credentials, cookies) for client connection * @param uriRemap * - mapping of Uris to avoid redirects on subsequent invocations * @return false if credentials are required and we should terminate * immediately. */ private boolean uploadOneSubmission(String urlString, Uri toUpdate, String id, String submissionInstanceId, FileSet instanceFiles, HttpClient httpclient, HttpContext localContext, Map<URI, URI> uriRemap) { ContentValues cv = new ContentValues(); cv.put(InstanceColumns.SUBMISSION_INSTANCE_ID, submissionInstanceId); URI u = null; try { URL url = new URL(URLDecoder.decode(urlString, CharEncoding.UTF_8)); u = url.toURI(); } catch (MalformedURLException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (URISyntaxException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid uri: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnsupportedEncodingException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } // NOTE: ODK Survey assumes you are interfacing with an // OpenRosa-compliant server if (uriRemap.containsKey(u)) { // we already issued a head request and got a response, // so we know the proper URL to send the submission to // and the proper scheme. We also know that it was an // OpenRosa compliant server. u = uriRemap.get(u); } else { // we need to issue a head request HttpHead httpHead = WebUtils.get().createOpenRosaHttpHead(u); // prepare response HttpResponse response = null; try { response = httpclient.execute(httpHead, localContext); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 401) { WebUtils.get().discardEntityBytes(response); // we need authentication, so stop and return what we've // done so far. mOutcome.mAuthRequestingServer = u; return false; } else if (statusCode == 204) { Header[] locations = response.getHeaders("Location"); WebUtils.get().discardEntityBytes(response); if (locations != null && locations.length == 1) { try { URL url = new URL(URLDecoder.decode(locations[0].getValue(), CharEncoding.UTF_8)); URI uNew = url.toURI(); if (u.getHost().equalsIgnoreCase(uNew.getHost())) { // trust the server to tell us a new location // ... and possibly to use https instead. uriRemap.put(u, uNew); u = uNew; } else { // Don't follow a redirection attempt to a // different host. // We can't tell if this is a spoof or not. mOutcome.mResults.put(id, fail + "Unexpected redirection attempt to a different host: " + uNew.toString()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + urlString + " " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } else { // may be a server that does not handle WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).w(t, "Status code on Head request: " + statusCode); if (statusCode >= 200 && statusCode <= 299) { mOutcome.mResults.put(id, fail + "Invalid status code on Head request. If you have a web proxy, you may need to login to your network. "); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } catch (ClientProtocolException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Client Protocol Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (ConnectTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnknownHostException e) { WebLogger.getLogger(appName).printStackTrace(e); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + e.getMessage() + " :: Network Connection Failed"); WebLogger.getLogger(appName).e(t, e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (SocketTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.getMessage()); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (HttpHostConnectException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.toString()); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Network Connection Refused"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Generic Exception"); WebLogger.getLogger(appName).e(t, e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // At this point, we may have updated the uri to use https. // This occurs only if the Location header keeps the host name // the same. If it specifies a different host name, we error // out. // // And we may have set authentication cookies in our // cookiestore (referenced by localContext) that will enable // authenticated publication to the server. // // get instance file File instanceFile = instanceFiles.instanceFile; if (!instanceFile.exists()) { mOutcome.mResults.put(id, fail + "instance XML file does not exist!"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } List<MimeFile> files = instanceFiles.attachmentFiles; boolean first = true; int j = 0; int lastJ; while (j < files.size() || first) { lastJ = j; first = false; HttpPost httppost = WebUtils.get().createOpenRosaHttpPost(u, mAuth); long byteCount = 0L; // mime post MultipartEntity entity = new MultipartEntity(); // add the submission file first... FileBody fb = new FileBody(instanceFile, "text/xml"); entity.addPart("xml_submission_file", fb); WebLogger.getLogger(appName).i(t, "added xml_submission_file: " + instanceFile.getName()); byteCount += instanceFile.length(); for (; j < files.size(); j++) { MimeFile mf = files.get(j); File f = mf.file; String contentType = mf.contentType; fb = new FileBody(f, contentType); entity.addPart(f.getName(), fb); byteCount += f.length(); WebLogger.getLogger(appName).i(t, "added " + contentType + " file " + f.getName()); // we've added at least one attachment to the request... if (j + 1 < files.size()) { long nextFileLength = (files.get(j + 1).file.length()); if ((j - lastJ + 1 > 100) || (byteCount + nextFileLength > 10000000L)) { // the next file would exceed the 10MB threshold... WebLogger.getLogger(appName).i(t, "Extremely long post is being split into multiple posts"); try { StringBody sb = new StringBody("yes", Charset.forName(CharEncoding.UTF_8)); entity.addPart("*isIncomplete*", sb); } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); // never happens... } ++j; // advance over the last attachment added... break; } } } httppost.setEntity(entity); // prepare response and return uploaded HttpResponse response = null; try { response = httpclient.execute(httppost, localContext); int responseCode = response.getStatusLine().getStatusCode(); WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).i(t, "Response code:" + responseCode); // verify that the response was a 201 or 202. // If it wasn't, the submission has failed. if (responseCode != 201 && responseCode != 202) { if (responseCode == 200) { mOutcome.mResults.put(id, fail + "Network login failure? Again?"); } else { mOutcome.mResults.put(id, fail + response.getStatusLine().getReasonPhrase() + " (" + responseCode + ") at " + urlString); } cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "Generic Exception. " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // if it got here, it must have worked mOutcome.mResults.put(id, appContext.getString(R.string.success)); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMITTED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; }
From source file:org.path.episample.android.tasks.InstanceUploaderTask.java
/** * Uploads to urlString the submission identified by id with filepath of * instance//from w ww . j a v a 2s.co m * * @param urlString * destination URL * @param id * -- _ID in the InstanceColumns table. * @param instanceFilePath * @param httpclient * - client connection * @param localContext * - context (e.g., credentials, cookies) for client connection * @param uriRemap * - mapping of Uris to avoid redirects on subsequent invocations * @return false if credentials are required and we should terminate * immediately. */ private boolean uploadOneSubmission(String urlString, Uri toUpdate, String id, String submissionInstanceId, FileSet instanceFiles, HttpClient httpclient, HttpContext localContext, Map<URI, URI> uriRemap) { ContentValues cv = new ContentValues(); cv.put(InstanceColumns.SUBMISSION_INSTANCE_ID, submissionInstanceId); URI u = null; try { URL url = new URL(URLDecoder.decode(urlString, CharEncoding.UTF_8)); u = url.toURI(); } catch (MalformedURLException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + "Malformed URL Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (URISyntaxException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid uri: " + urlString + " :: details: " + "URI Syntax Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnsupportedEncodingException e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "invalid url: " + urlString + " :: details: " + "Unsupported Encoding Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } // NOTE: ODK Survey assumes you are interfacing with an // OpenRosa-compliant server if (uriRemap.containsKey(u)) { // we already issued a head request and got a response, // so we know the proper URL to send the submission to // and the proper scheme. We also know that it was an // OpenRosa compliant server. u = uriRemap.get(u); } else { // we need to issue a head request HttpHead httpHead = WebUtils.get().createOpenRosaHttpHead(u); // prepare response HttpResponse response = null; try { response = httpclient.execute(httpHead, localContext); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 401) { WebUtils.get().discardEntityBytes(response); // we need authentication, so stop and return what we've // done so far. mOutcome.mAuthRequestingServer = u; return false; } else if (statusCode == 204) { Header[] locations = response.getHeaders("Location"); WebUtils.get().discardEntityBytes(response); if (locations != null && locations.length == 1) { try { URL url = new URL(URLDecoder.decode(locations[0].getValue(), CharEncoding.UTF_8)); URI uNew = url.toURI(); if (u.getHost().equalsIgnoreCase(uNew.getHost())) { // trust the server to tell us a new location // ... and possibly to use https instead. uriRemap.put(u, uNew); u = uNew; } else { // Don't follow a redirection attempt to a // different host. // We can't tell if this is a spoof or not. mOutcome.mResults.put(id, fail + "Unexpected redirection attempt to a different host: " + uNew.toString()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + urlString + " " + "Generic Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } else { // may be a server that does not handle WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).w(t, "Status code on Head request: " + statusCode); if (statusCode >= 200 && statusCode <= 299) { mOutcome.mResults.put(id, fail + "Invalid status code on Head request. If you have a web proxy, you may need to login to your network. "); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } } catch (ClientProtocolException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, "Client Protocol Exception"); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Client Protocol Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (ConnectTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, "Connect Timeout Exception"); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (UnknownHostException e) { WebLogger.getLogger(appName).printStackTrace(e); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "UnknownvHost Exception" + " :: Network Connection Failed"); WebLogger.getLogger(appName).e(t, "UnknownvHost Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (SocketTimeoutException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, "Socket Timeout Exception"); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Connection Timeout"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (HttpHostConnectException e) { WebLogger.getLogger(appName).printStackTrace(e); WebLogger.getLogger(appName).e(t, e.toString()); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Network Connection Refused"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); ClientConnectionManagerFactory.get(appName).clearHttpConnectionManager(); mOutcome.mResults.put(id, fail + "Generic Exception"); WebLogger.getLogger(appName).e(t, "Generic Exception"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // At this point, we may have updated the uri to use https. // This occurs only if the Location header keeps the host name // the same. If it specifies a different host name, we error // out. // // And we may have set authentication cookies in our // cookiestore (referenced by localContext) that will enable // authenticated publication to the server. // // get instance file File instanceFile = instanceFiles.instanceFile; if (!instanceFile.exists()) { mOutcome.mResults.put(id, fail + "instance XML file does not exist!"); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } List<MimeFile> files = instanceFiles.attachmentFiles; boolean first = true; int j = 0; int lastJ; while (j < files.size() || first) { lastJ = j; first = false; HttpPost httppost = WebUtils.get().createOpenRosaHttpPost(u, mAuth); long byteCount = 0L; // mime post MultipartEntity entity = new MultipartEntity(); // add the submission file first... FileBody fb = new FileBody(instanceFile, "text/xml"); entity.addPart("xml_submission_file", fb); WebLogger.getLogger(appName).i(t, "added xml_submission_file: " + instanceFile.getName()); byteCount += instanceFile.length(); for (; j < files.size(); j++) { MimeFile mf = files.get(j); File f = mf.file; String contentType = mf.contentType; fb = new FileBody(f, contentType); entity.addPart(f.getName(), fb); byteCount += f.length(); WebLogger.getLogger(appName).i(t, "added " + contentType + " file " + f.getName()); // we've added at least one attachment to the request... if (j + 1 < files.size()) { long nextFileLength = (files.get(j + 1).file.length()); if ((j - lastJ + 1 > 100) || (byteCount + nextFileLength > 10000000L)) { // the next file would exceed the 10MB threshold... WebLogger.getLogger(appName).i(t, "Extremely long post is being split into multiple posts"); try { StringBody sb = new StringBody("yes", Charset.forName(CharEncoding.UTF_8)); entity.addPart("*isIncomplete*", sb); } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); // never happens... } ++j; // advance over the last attachment added... break; } } } httppost.setEntity(entity); // prepare response and return uploaded HttpResponse response = null; try { response = httpclient.execute(httppost, localContext); int responseCode = response.getStatusLine().getStatusCode(); WebUtils.get().discardEntityBytes(response); WebLogger.getLogger(appName).i(t, "Response code:" + responseCode); // verify that the response was a 201 or 202. // If it wasn't, the submission has failed. if (responseCode != 201 && responseCode != 202) { if (responseCode == 200) { mOutcome.mResults.put(id, fail + "Network login failure? Again?"); } else { mOutcome.mResults.put(id, fail + response.getStatusLine().getReasonPhrase() + " (" + responseCode + ") at " + urlString); } cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } catch (Exception e) { WebLogger.getLogger(appName).printStackTrace(e); mOutcome.mResults.put(id, fail + "Generic Exception. " + e.getMessage()); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMISSION_FAILED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; } } // if it got here, it must have worked mOutcome.mResults.put(id, appContext.getString(R.string.success)); cv.put(InstanceColumns.XML_PUBLISH_STATUS, InstanceColumns.STATUS_SUBMITTED); appContext.getContentResolver().update(toUpdate, cv, null, null); return true; }