List of usage examples for java.net HttpURLConnection getURL
public URL getURL()
From source file:org.apache.streams.urls.LinkResolver.java
public void unwindLink(String url) { Objects.requireNonNull(linkDetails); Objects.requireNonNull(url);//w ww .ja va 2s. c om // Check url validity UrlValidator urlValidator = new UrlValidator(); if (!urlValidator.isValid(url)) { linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL); return; } // Check to see if they wound up in a redirect loop, // IE: 'A' redirects to 'B', then 'B' redirects to 'A' if ((linkDetails.getRedirectCount() != null && linkDetails.getRedirectCount() > 0 && (linkDetails.getOriginalURL().equals(url) || linkDetails.getRedirects().contains(url))) || (linkDetails.getRedirectCount() != null && linkDetails.getRedirectCount() > MAX_ALLOWED_REDIRECTS)) { linkDetails.setLinkStatus(LinkDetails.LinkStatus.LOOP); return; } if (!linkDetails.getOriginalURL().equals(url)) linkDetails.getRedirects().add(url); HttpURLConnection connection = null; // Store where the redirected link will go (if there is one) String reDirectedLink = null; try { // Turn the string into a URL URL thisURL = new URL(url); // Be sensitive to overloading domains STREAMS-77 try { String host = thisURL.getHost().toLowerCase(); if (!domainsSensitiveTo.contains(host)) { domainsSensitiveTo.add(host); long domainWait = LinkResolverHelperFunctions.waitTimeForDomain(thisURL.getHost()); if (domainWait > 0) { LOGGER.debug("Waiting for domain: {}", domainWait); Thread.sleep(domainWait); } } } catch (Exception e) { // noOp } connection = (HttpURLConnection) new URL(url).openConnection(); // now we are going to pretend that we are a browser... // This is the way my mac works. if (!BOTS_ARE_OK.contains(thisURL.getHost())) { connection.addRequestProperty("Host", thisURL.getHost()); // Bots are not 'ok', so we need to spoof the headers for (String k : SPOOF_HTTP_HEADERS.keySet()) connection.addRequestProperty(k, SPOOF_HTTP_HEADERS.get(k)); // the test to seattlemamadoc.com prompted this change. // they auto detect bots by checking the referrer chain and the 'user-agent' // this broke the t.co test. t.co URLs are EXPLICITLY ok with bots // there is a list for URLS that behave this way at the top in BOTS_ARE_OK // smashew 2013-13-2013 if (linkDetails.getRedirectCount() > 0 && BOTS_ARE_OK.contains(thisURL.getHost())) connection.addRequestProperty("Referrer", linkDetails.getOriginalURL()); } connection.setReadTimeout(DEFAULT_HTTP_TIMEOUT); connection.setConnectTimeout(DEFAULT_HTTP_TIMEOUT); // we want to follow this behavior on our own to ensure that we are getting to the // proper place. This is especially true with links that are wounded by special // link winders, // IE: connection.setInstanceFollowRedirects(false); if (linkDetails.getCookies() != null) for (String cookie : linkDetails.getCookies()) connection.addRequestProperty("Cookie", cookie.split(";", 1)[0]); connection.connect(); linkDetails.setFinalResponseCode((long) connection.getResponseCode()); Map<String, List<String>> headers = createCaseInsensitiveMap(connection.getHeaderFields()); /* * If they want us to set cookies, well, then we will set cookies * Example URL: * http://nyti.ms/1bCpesx *****************************************************************/ if (headers.containsKey(SET_COOKIE_IDENTIFIER)) linkDetails.getCookies().add(headers.get(SET_COOKIE_IDENTIFIER).get(0)); switch (linkDetails.getFinalResponseCode().intValue()) { /* * W3C HTTP Response Codes: * http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html */ case 200: // HTTP OK linkDetails.setFinalURL(connection.getURL().toString()); linkDetails.setDomain(new URL(linkDetails.getFinalURL()).getHost()); linkDetails.setLinkStatus(LinkDetails.LinkStatus.SUCCESS); break; case 300: // Multiple choices case 301: // URI has been moved permanently case 302: // Found case 303: // Primarily for a HTTP Post case 304: // Not Modified case 306: // This status code is unused but in the redirect block. case 307: // Temporary re-direct /* * Author: * Smashew * * Date: 2013-11-15 * * Note: * It is possible that we have already found our final URL. In * the event that we have found our final URL, we are going to * save this URL as long as it isn't the original URL. * We are still going to ask the browser to re-direct, but in the * case of yet another redirect, seen with the redbull test * this can be followed by a 304, a browser, by W3C standards would * still render the page with it's content, but for us to assert * a success, we are really hoping for a 304 message. *******************************************************************/ if (!linkDetails.getOriginalURL().toLowerCase() .equals(connection.getURL().toString().toLowerCase())) linkDetails.setFinalURL(connection.getURL().toString()); if (!headers.containsKey(LOCATION_IDENTIFIER)) { LOGGER.info("Headers: {}", headers); linkDetails.setLinkStatus(LinkDetails.LinkStatus.REDIRECT_ERROR); } else { linkDetails.setRedirected(Boolean.TRUE); linkDetails.setRedirectCount(linkDetails.getRedirectCount() + 1); reDirectedLink = connection.getHeaderField(LOCATION_IDENTIFIER); } break; case 305: // User must use the specified proxy (deprecated by W3C) break; case 401: // Unauthorized (nothing we can do here) linkDetails.setLinkStatus(LinkDetails.LinkStatus.UNAUTHORIZED); break; case 403: // HTTP Forbidden (Nothing we can do here) linkDetails.setLinkStatus(LinkDetails.LinkStatus.FORBIDDEN); break; case 404: // Not Found (Page is not found, nothing we can do with a 404) linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND); break; case 500: // Internal Server Error case 501: // Not Implemented case 502: // Bad Gateway case 503: // Service Unavailable case 504: // Gateway Timeout case 505: // Version not supported linkDetails.setLinkStatus(LinkDetails.LinkStatus.HTTP_ERROR_STATUS); break; default: LOGGER.info("Unrecognized HTTP Response Code: {}", linkDetails.getFinalResponseCode()); linkDetails.setLinkStatus(LinkDetails.LinkStatus.NOT_FOUND); break; } } catch (MalformedURLException e) { // the URL is trash, so, it can't load it. linkDetails.setLinkStatus(LinkDetails.LinkStatus.MALFORMED_URL); } catch (IOException ex) { // there was an issue we are going to set to error. linkDetails.setLinkStatus(LinkDetails.LinkStatus.ERROR); } catch (Exception ex) { // there was an unknown issue we are going to set to exception. linkDetails.setLinkStatus(LinkDetails.LinkStatus.EXCEPTION); } finally { // if the connection is not null, then we need to disconnect to close any underlying resources if (connection != null) connection.disconnect(); } // If there was a redirection, then we have to keep going // Placing this code here should help to satisfy ensuring that the connection object // is closed successfully. if (reDirectedLink != null) unwindLink(reDirectedLink); }
From source file:com.BeatYourRecord.SubmitActivity.java
private String uploadMetaData(String filePath, boolean retry) throws IOException { String uploadUrl = INITIAL_UPLOAD_URL; HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true);/* ww w.j a va2 s . co m*/ urlConnection.setRequestProperty("Content-Type", "application/atom+xml"); urlConnection.setRequestProperty("Slug", filePath); String atomData; int pos = filePath.indexOf("BYR"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd"); String currentDateandTime = sdf.format(new Date()); SharedPreferences pref1 = SubmitActivity.this.getSharedPreferences("TourPref", 0); // 0 - for private mode Editor editor1 = pref1.edit(); String username = pref1.getString("Username", null); String title = "BeatYourRecord Free Throws " + currentDateandTime + " Score : " + getTitleText() + " User : " + username; //this.initFile().getAbsolutePath(); String description = "Beat Your Record Free Throw Tournament. User : " + username + ". Go to" + " www.ec2-54-212-221-3.us-west-2.compute.amazonaws.com" + " to take part in our other tournaments"; String category = DEFAULT_VIDEO_CATEGORY; this.tags = DEFAULT_VIDEO_TAGS; if (!Util.isNullOrEmpty(this.getTagsText())) { this.tags = this.getTagsText(); } if (this.videoLocation == null) { String template = Util.readFile(this, R.raw.gdata).toString(); atomData = String.format(template, title, description, category, this.tags); } else { String template = Util.readFile(this, R.raw.gdata_geo).toString(); atomData = String.format(template, title, description, category, this.tags, videoLocation.getLatitude(), videoLocation.getLongitude()); } OutputStreamWriter outStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream()); outStreamWriter.write(atomData); outStreamWriter.close(); int responseCode = urlConnection.getResponseCode(); if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry) { Log.d(LOG_TAG, "retrying to fetch auth token for " + youTubeName); this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken); // Try again with fresh token return uploadMetaData(filePath, false); } else { throw new IOException(String.format("response code='%s' (code %d)" + " for %s", urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); } } return urlConnection.getHeaderField("Location"); }
From source file:com.wso2telco.dep.mediator.RequestExecutor.java
/** * Make request./*from w ww. j av a 2s. co m*/ * * @param operatorendpoint * the operatorendpoint * @param url * the url * @param requestStr * the request str * @param auth * the auth * @param messageContext * the message context * @return the string */ public String makeRequest(OperatorEndpoint operatorendpoint, String url, String requestStr, boolean auth, MessageContext messageContext, boolean inclueHeaders) { //MO Callback boolean isMoCallBack = false; JSONObject jsonObject = null; try { jsonObject = new JSONObject(requestStr); } catch (JSONException error) { error.printStackTrace(); } Iterator<String> keys = jsonObject.keys(); if (keys.hasNext()) { String key = (String) keys.next(); if (key.equals("inboundSMSMessageNotification") || key.equals("deliveryInfoNotification")) { isMoCallBack = true; } } try {// check for charge operation. if true append ESB url JSONObject jsonObj = new JSONObject(requestStr); String transactionOperationStatus = jsonObj.getJSONObject("amountTransaction") .getString("transactionOperationStatus"); String status = "Charged"; if (status.equals(transactionOperationStatus)) { url = modifyEndpoint(url, operatorendpoint.getOperator(), messageContext); } } catch (JSONException ignore) { } ICallresponse icallresponse = null; String retStr = ""; int statusCode = 0; URL neturl; HttpURLConnection connection = null; try { // String Authtoken = AccessToken; // //FileUtil.getApplicationProperty("wow.api.bearer.token"); // String encodeurl = URLEncoder.encode(url, "UTF-8"); neturl = new URL(url); connection = (HttpURLConnection) neturl.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Accept", "application/json"); //connection.setRequestProperty("charset", "utf-8"); if (auth) { connection.setRequestProperty("Authorization", "Bearer " + getAccessToken(operatorendpoint.getOperator(), messageContext)); // Add JWT token header org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext) .getAxis2MessageContext(); Object headers = axis2MessageContext .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); if (headers != null && headers instanceof Map) { Map headersMap = (Map) headers; String jwtparam = (String) headersMap.get("x-jwt-assertion"); if (jwtparam != null) { connection.setRequestProperty("x-jwt-assertion", jwtparam); } } } if (inclueHeaders) { org.apache.axis2.context.MessageContext axis2MessageContext = ((Axis2MessageContext) messageContext) .getAxis2MessageContext(); Object headers = axis2MessageContext .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); if (headers != null && headers instanceof Map) { Map headersMap = (Map) headers; Iterator it = headersMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); connection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); // avoids a // ConcurrentModificationException } } } connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); log.info("Southbound Request URL: " + connection.getRequestMethod() + " " + connection.getURL() + " Request ID: " + UID.getRequestID(messageContext)); if (log.isDebugEnabled()) { log.debug("Southbound Request Headers: " + connection.getRequestProperties()); } log.info("Southbound Request Body: " + requestStr + " Request ID: " + UID.getRequestID(messageContext)); //========================UNICODE PATCH========================================= BufferedOutputStream wr = new BufferedOutputStream(connection.getOutputStream()); wr.write(requestStr.getBytes("UTF-8")); wr.flush(); wr.close(); //========================UNICODE PATCH========================================= /*DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(requestStr); wr.flush(); wr.close();*/ statusCode = connection.getResponseCode(); if ((statusCode != 200) && (statusCode != 201) && (statusCode != 400) && (statusCode != 401)) { throw new RuntimeException("Failed : HTTP error code : " + statusCode); } InputStream is = null; if ((statusCode == 200) || (statusCode == 201)) { is = connection.getInputStream(); } else { is = connection.getErrorStream(); } BufferedReader br = new BufferedReader(new InputStreamReader(is)); String output; while ((output = br.readLine()) != null) { retStr += output; } br.close(); log.info("Southbound Response Status: " + statusCode + " " + connection.getResponseMessage() + " Request ID: " + UID.getRequestID(messageContext)); if (log.isDebugEnabled()) { log.debug("Southbound Response Headers: " + connection.getHeaderFields()); } log.info("Southbound Response Body: " + retStr + " Request ID: " + UID.getRequestID(messageContext)); } catch (Exception e) { log.error("[WSRequestService ], makerequest, " + e.getMessage(), e); return null; } finally { if (connection != null) { connection.disconnect(); } log.debug( "Mo OR DN CallBack : " + isMoCallBack + " requestStr : " + requestStr + " retStr : " + retStr); messageContext.setProperty(DataPublisherConstants.RESPONSE_CODE, Integer.toString(statusCode)); messageContext.setProperty(DataPublisherConstants.MSISDN, messageContext.getProperty(MSISDNConstants.USER_MSISDN)); /* TODO:This need to consider when publishing request data if (isMoCallBack) { publishResponseData(statusCode, requestStr, messageContext); }else { publishResponseData(statusCode, retStr, messageContext); }*/ } return retStr; }
From source file:org.m2x.rssreader.service.FetcherService.java
private int refreshFeed(String feedId) { RssAtomParser handler = null;/*from w w w . ja v a2s .c o m*/ ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null); if (!cursor.moveToFirst()) { cursor.close(); return 0; } int urlPosition = cursor.getColumnIndex(FeedColumns.URL); int titlePosition = cursor.getColumnIndex(FeedColumns.NAME); int fetchmodePosition = cursor.getColumnIndex(FeedColumns.FETCH_MODE); int realLastUpdatePosition = cursor.getColumnIndex(FeedColumns.REAL_LAST_UPDATE); int iconPosition = cursor.getColumnIndex(FeedColumns.ICON); int retrieveFullTextPosition = cursor.getColumnIndex(FeedColumns.RETRIEVE_FULLTEXT); HttpURLConnection connection = null; try { String feedUrl = cursor.getString(urlPosition); connection = NetworkUtils.setupConnection(feedUrl); String contentType = connection.getContentType(); int fetchMode = cursor.getInt(fetchmodePosition); handler = new RssAtomParser(new Date(cursor.getLong(realLastUpdatePosition)), feedId, cursor.getString(titlePosition), feedUrl, cursor.getInt(retrieveFullTextPosition) == 1); handler.setFetchImages(PrefUtils.getBoolean(PrefUtils.FETCH_PICTURES, true)); if (fetchMode == 0) { if (contentType != null && contentType.startsWith(CONTENT_TYPE_TEXT_HTML)) { BufferedReader reader = new BufferedReader( new InputStreamReader(NetworkUtils.getConnectionInputStream(connection))); String line; int posStart = -1; while ((line = reader.readLine()) != null) { if (line.contains(HTML_BODY)) { break; } else { Matcher matcher = FEED_LINK_PATTERN.matcher(line); if (matcher.find()) { // not "while" as only one link is needed line = matcher.group(); posStart = line.indexOf(HREF); if (posStart > -1) { String url = line.substring(posStart + 6, line.indexOf('"', posStart + 10)) .replace("&", "&"); ContentValues values = new ContentValues(); if (url.startsWith("/")) { int index = feedUrl.indexOf('/', 8); if (index > -1) { url = feedUrl.substring(0, index) + url; } else { url = feedUrl + url; } } else if (!url.startsWith(Constants.HTTP_SCHEME) && !url.startsWith(Constants.HTTPS_SCHEME)) { url = feedUrl + '/' + url; } values.put(FeedColumns.URL, url); cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null); connection.disconnect(); connection = NetworkUtils.setupConnection(url); contentType = connection.getContentType(); break; } } } } // this indicates a badly configured feed if (posStart == -1) { connection.disconnect(); connection = NetworkUtils.setupConnection(feedUrl); contentType = connection.getContentType(); } } if (contentType != null) { int index = contentType.indexOf(CHARSET); if (index > -1) { int index2 = contentType.indexOf(';', index); try { Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8)); fetchMode = FETCHMODE_DIRECT; } catch (UnsupportedEncodingException usee) { fetchMode = FETCHMODE_REENCODE; } } else { fetchMode = FETCHMODE_REENCODE; } } else { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(NetworkUtils.getConnectionInputStream(connection))); char[] chars = new char[20]; int length = bufferedReader.read(chars); String xmlDescription = new String(chars, 0, length); connection.disconnect(); connection = NetworkUtils.setupConnection(connection.getURL()); int start = xmlDescription != null ? xmlDescription.indexOf(ENCODING) : -1; if (start > -1) { try { Xml.findEncodingByName( xmlDescription.substring(start + 10, xmlDescription.indexOf('"', start + 11))); fetchMode = FETCHMODE_DIRECT; } catch (UnsupportedEncodingException usee) { fetchMode = FETCHMODE_REENCODE; } } else { // absolutely no encoding information found fetchMode = FETCHMODE_DIRECT; } } ContentValues values = new ContentValues(); values.put(FeedColumns.FETCH_MODE, fetchMode); cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null); } switch (fetchMode) { default: case FETCHMODE_DIRECT: { if (contentType != null) { int index = contentType.indexOf(CHARSET); int index2 = contentType.indexOf(';', index); InputStream inputStream = NetworkUtils.getConnectionInputStream(connection); Xml.parse(inputStream, Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8)), handler); } else { InputStreamReader reader = new InputStreamReader( NetworkUtils.getConnectionInputStream(connection)); Xml.parse(reader, handler); } break; } case FETCHMODE_REENCODE: { ByteArrayOutputStream ouputStream = new ByteArrayOutputStream(); InputStream inputStream = NetworkUtils.getConnectionInputStream(connection); byte[] byteBuffer = new byte[4096]; int n; while ((n = inputStream.read(byteBuffer)) > 0) { ouputStream.write(byteBuffer, 0, n); } String xmlText = ouputStream.toString(); int start = xmlText != null ? xmlText.indexOf(ENCODING) : -1; if (start > -1) { Xml.parse(new StringReader(new String(ouputStream.toByteArray(), xmlText.substring(start + 10, xmlText.indexOf('"', start + 11)))), handler); } else { // use content type if (contentType != null) { int index = contentType.indexOf(CHARSET); if (index > -1) { int index2 = contentType.indexOf(';', index); try { StringReader reader = new StringReader(new String(ouputStream.toByteArray(), index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8))); Xml.parse(reader, handler); } catch (Exception ignored) { } } else { StringReader reader = new StringReader(new String(ouputStream.toByteArray())); Xml.parse(reader, handler); } } } break; } } connection.disconnect(); } catch (FileNotFoundException e) { if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) { ContentValues values = new ContentValues(); // resets the fetchmode to determine it again later values.put(FeedColumns.FETCH_MODE, 0); values.put(FeedColumns.ERROR, getString(R.string.error_feed_error)); cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null); } } catch (Throwable e) { if (handler == null || (handler != null && !handler.isDone() && !handler.isCancelled())) { ContentValues values = new ContentValues(); // resets the fetchmode to determine it again later values.put(FeedColumns.FETCH_MODE, 0); values.put(FeedColumns.ERROR, e.getMessage() != null ? e.getMessage() : getString(R.string.error_feed_process)); cr.update(FeedColumns.CONTENT_URI(feedId), values, null, null); } } finally { /* check and optionally find favicon */ try { if (handler != null && cursor.getBlob(iconPosition) == null) { String feedLink = handler.getFeedLink(); if (feedLink != null) { NetworkUtils.retrieveFavicon(this, new URL(feedLink), feedId); } else { NetworkUtils.retrieveFavicon(this, connection.getURL(), feedId); } } } catch (Throwable ignored) { } if (connection != null) { connection.disconnect(); } } cursor.close(); return handler != null ? handler.getNewCount() : 0; }
From source file:nl.privacybarometer.privacyvandaag.service.FetcherService.java
private int refreshFeed(String feedId, long keepDateBorderTime) { RssAtomParser handler = null;/*from w ww. j av a 2 s .c o m*/ ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(FeedColumns.CONTENT_URI(feedId), null, null, null, null); if (cursor.moveToFirst()) { int urlPosition = cursor.getColumnIndex(FeedColumns.URL); int idPosition = cursor.getColumnIndex(FeedColumns._ID); int titlePosition = cursor.getColumnIndex(FeedColumns.NAME); int fetchModePosition = cursor.getColumnIndex(FeedColumns.FETCH_MODE); int realLastUpdatePosition = cursor.getColumnIndex(FeedColumns.REAL_LAST_UPDATE); int iconPosition = cursor.getColumnIndex(FeedColumns.ICON); int retrieveFullscreenPosition = cursor.getColumnIndex(FeedColumns.RETRIEVE_FULLTEXT); /* ModPrivacyVandaag: if Fetchmode = 99, do not refresh this feed. */ int fetchMode = cursor.getInt(fetchModePosition); if (fetchMode == FETCHMODE_DO_NOT_FETCH) { cursor.close(); return 0; } // end of this added block of code; commented out initialize of fetchmode on line 520 String id = cursor.getString(idPosition); HttpURLConnection connection = null; try { String feedUrl = cursor.getString(urlPosition); connection = NetworkUtils.setupConnection(feedUrl); String contentType = connection.getContentType(); handler = new RssAtomParser(new Date(cursor.getLong(realLastUpdatePosition)), keepDateBorderTime, id, cursor.getString(titlePosition), feedUrl, cursor.getInt(retrieveFullscreenPosition) == 1); handler.setFetchImages(NetworkUtils.needDownloadPictures()); // Log.e (TAG,"feedUrl = "+feedUrl); if (fetchMode == 0) { if (contentType != null && contentType.startsWith(CONTENT_TYPE_TEXT_HTML)) { BufferedReader reader = new BufferedReader( new InputStreamReader(connection.getInputStream())); String line; int posStart = -1; while ((line = reader.readLine()) != null) { if (line.contains(HTML_BODY)) { break; } else { Matcher matcher = FEED_LINK_PATTERN.matcher(line); if (matcher.find()) { // not "while" as only one link is needed line = matcher.group(); posStart = line.indexOf(HREF); if (posStart > -1) { String url = line.substring(posStart + 6, line.indexOf('"', posStart + 10)) .replace(Constants.AMP_SG, Constants.AMP); ContentValues values = new ContentValues(); if (url.startsWith(Constants.SLASH)) { int index = feedUrl.indexOf('/', 8); if (index > -1) { url = feedUrl.substring(0, index) + url; } else { url = feedUrl + url; } } else if (!url.startsWith(Constants.HTTP_SCHEME) && !url.startsWith(Constants.HTTPS_SCHEME)) { url = feedUrl + '/' + url; } values.put(FeedColumns.URL, url); cr.update(FeedColumns.CONTENT_URI(id), values, null, null); connection.disconnect(); connection = NetworkUtils.setupConnection(url); contentType = connection.getContentType(); break; } } } } // this indicates a badly configured feed if (posStart == -1) { connection.disconnect(); connection = NetworkUtils.setupConnection(feedUrl); contentType = connection.getContentType(); } } if (contentType != null) { int index = contentType.indexOf(CHARSET); if (index > -1) { int index2 = contentType.indexOf(';', index); try { Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8)); fetchMode = FETCHMODE_DIRECT; } catch (UnsupportedEncodingException ignored) { fetchMode = FETCHMODE_REENCODE; } } else { fetchMode = FETCHMODE_REENCODE; } } else { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(connection.getInputStream())); char[] chars = new char[20]; int length = bufferedReader.read(chars); String xmlDescription = new String(chars, 0, length); connection.disconnect(); connection = NetworkUtils.setupConnection(connection.getURL()); int start = xmlDescription.indexOf(ENCODING); if (start > -1) { try { Xml.findEncodingByName(xmlDescription.substring(start + 10, xmlDescription.indexOf('"', start + 11))); fetchMode = FETCHMODE_DIRECT; } catch (UnsupportedEncodingException ignored) { fetchMode = FETCHMODE_REENCODE; } } else { // absolutely no encoding information found fetchMode = FETCHMODE_DIRECT; } } ContentValues values = new ContentValues(); values.put(FeedColumns.FETCH_MODE, fetchMode); cr.update(FeedColumns.CONTENT_URI(id), values, null, null); } switch (fetchMode) { default: case FETCHMODE_DIRECT: { if (contentType != null) { int index = contentType.indexOf(CHARSET); int index2 = contentType.indexOf(';', index); InputStream inputStream = connection.getInputStream(); Xml.parse(inputStream, Xml.findEncodingByName(index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8)), handler); } else { InputStreamReader reader = new InputStreamReader(connection.getInputStream()); Xml.parse(reader, handler); } break; } case FETCHMODE_REENCODE: { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); InputStream inputStream = connection.getInputStream(); byte[] byteBuffer = new byte[4096]; int n; while ((n = inputStream.read(byteBuffer)) > 0) { outputStream.write(byteBuffer, 0, n); } String xmlText = outputStream.toString(); int start = xmlText != null ? xmlText.indexOf(ENCODING) : -1; if (start > -1) { Xml.parse(new StringReader(new String(outputStream.toByteArray(), xmlText.substring(start + 10, xmlText.indexOf('"', start + 11)))), handler); } else { // use content type if (contentType != null) { int index = contentType.indexOf(CHARSET); if (index > -1) { int index2 = contentType.indexOf(';', index); try { StringReader reader = new StringReader(new String(outputStream.toByteArray(), index2 > -1 ? contentType.substring(index + 8, index2) : contentType.substring(index + 8))); Xml.parse(reader, handler); } catch (Exception e) { Log.e("Privacy Vandaag: ", "Error reading string " + e.getMessage()); } } else { StringReader reader = new StringReader(xmlText); Xml.parse(reader, handler); } } } break; } } connection.disconnect(); } catch (FileNotFoundException e) { if (handler == null || (!handler.isDone() && !handler.isCancelled())) { ContentValues values = new ContentValues(); // resets the fetch mode to determine it again later values.put(FeedColumns.FETCH_MODE, 0); values.put(FeedColumns.ERROR, getString(R.string.error_feed_error)); cr.update(FeedColumns.CONTENT_URI(id), values, null, null); } } catch (Throwable e) { if (handler == null || (!handler.isDone() && !handler.isCancelled())) { ContentValues values = new ContentValues(); // resets the fetch mode to determine it again later values.put(FeedColumns.FETCH_MODE, 0); values.put(FeedColumns.ERROR, e.getMessage() != null ? e.getMessage() : getString(R.string.error_feed_process)); cr.update(FeedColumns.CONTENT_URI(id), values, null, null); handler = null; // If an error has occurred, reset the new articles counter for this feed to avoid notifications. } } finally { /* check and optionally find favicon */ /* No longer needed, because the icons of the feeds are included in the package */ /* try { if (handler != null && cursor.getBlob(iconPosition) == null) { String feedLink = handler.getFeedLink(); if (feedLink != null) { NetworkUtils.retrieveFavicon(this, new URL(feedLink), id); } else { NetworkUtils.retrieveFavicon(this, connection.getURL(), id); } } } catch (Throwable ignored) { } */ if (connection != null) { connection.disconnect(); } } } cursor.close(); int newArticles = (handler != null) ? handler.getNewCount() : 0; //Log.e(TAG, "Test notification is gegeven voor feedID " + feedId); //if (newArticles == 0 ) newArticles =2; // ONLY FOR TESTING !!!! // Check of meldingen voor deze feed aanstaat, anders newArticles op 0 zetten if (newArticles > 0) { boolean notifyFeed = true; switch (Integer.parseInt(feedId)) { case 1: // feedID Privacy Barometer notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_PRIVACYBAROMETER, true); break; case 2: // feedID Bits of Freedom notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_BITSOFFREEDOM, true); break; case 3: // feedID Privacy First notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_PRIVACYFIRST, true); break; case 4: // feedID Autoriteit Persoonsgegevens notifyFeed = PrefUtils.getBoolean(PrefUtils.NOTIFY_AUTORITEITPERSOONSGEGEVENS, true); } if (!notifyFeed) newArticles = 0; // geen melding als de meldingen voor deze feed uitstaan. } //Log.e(TAG, "Nieuwe artikelen is " + newArticles); return newArticles; }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java
/** * Samples the URL passed in and stores the result in * <code>HTTPSampleResult</code>, following redirects and downloading * page resources as appropriate./* w w w. j av a 2 s . co m*/ * <p> * When getting a redirect target, redirects are not followed and resources * are not downloaded. The caller will take care of this. * * @param url * URL to sample * @param method * HTTP method: GET, POST,... * @param areFollowingRedirect * whether we're getting a redirect target * @param frameDepth * Depth of this target in the frame structure. Used only to * prevent infinite recursion. * @return results of the sampling */ @Override protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) { HttpURLConnection conn = null; String urlStr = url.toString(); if (log.isDebugEnabled()) { log.debug("Start : sample " + urlStr); log.debug("method " + method + " followingRedirect " + areFollowingRedirect + " depth " + frameDepth); } HTTPSampleResult res = new HTTPSampleResult(); res.setMonitor(isMonitor()); res.setSampleLabel(urlStr); res.setURL(url); res.setHTTPMethod(method); res.sampleStart(); // Count the retries as well in the time // Check cache for an entry with an Expires header in the future final CacheManager cacheManager = getCacheManager(); if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) { if (cacheManager.inCache(url)) { return updateSampleResultForResourceInCache(res); } } try { // Sampling proper - establish the connection and read the response: // Repeatedly try to connect: int retry = -1; // Start with -1 so tries at least once, and retries at most MAX_CONN_RETRIES times for (; retry < MAX_CONN_RETRIES; retry++) { try { conn = setupConnection(url, method, res); // Attempt the connection: savedConn = conn; conn.connect(); break; } catch (BindException e) { if (retry >= MAX_CONN_RETRIES) { log.error("Can't connect after " + retry + " retries, " + e); throw e; } log.debug("Bind exception, try again"); if (conn != null) { savedConn = null; // we don't want interrupt to try disconnection again conn.disconnect(); } setUseKeepAlive(false); } catch (IOException e) { log.debug("Connection failed, giving up"); throw e; } } if (retry > MAX_CONN_RETRIES) { // This should never happen, but... throw new BindException(); } // Nice, we've got a connection. Finish sending the request: if (method.equals(HTTPConstants.POST)) { String postBody = sendPostData(conn); res.setQueryString(postBody); } else if (method.equals(HTTPConstants.PUT)) { String putBody = sendPutData(conn); res.setQueryString(putBody); } // Request sent. Now get the response: byte[] responseData = readResponse(conn, res); res.sampleEnd(); // Done with the sampling proper. // Now collect the results into the HTTPSampleResult: res.setResponseData(responseData); int errorLevel = conn.getResponseCode(); String respMsg = conn.getResponseMessage(); String hdr = conn.getHeaderField(0); if (hdr == null) { hdr = "(null)"; // $NON-NLS-1$ } if (errorLevel == -1) {// Bug 38902 - sometimes -1 seems to be returned unnecessarily if (respMsg != null) {// Bug 41902 - NPE try { errorLevel = Integer.parseInt(respMsg.substring(0, 3)); log.warn("ResponseCode==-1; parsed " + respMsg + " as " + errorLevel); } catch (NumberFormatException e) { log.warn("ResponseCode==-1; could not parse " + respMsg + " hdr: " + hdr); } } else { respMsg = hdr; // for result log.warn("ResponseCode==-1 & null ResponseMessage. Header(0)= " + hdr); } } if (errorLevel == -1) { res.setResponseCode("(null)"); // $NON-NLS-1$ } else { res.setResponseCode(Integer.toString(errorLevel)); } res.setSuccessful(isSuccessCode(errorLevel)); if (respMsg == null) {// has been seen in a redirect respMsg = hdr; // use header (if possible) if no message found } res.setResponseMessage(respMsg); String ct = conn.getContentType(); if (ct != null) { res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1 res.setEncodingAndType(ct); } String responseHeaders = getResponseHeaders(conn); res.setResponseHeaders(responseHeaders); if (res.isRedirect()) { res.setRedirectLocation(conn.getHeaderField(HTTPConstants.HEADER_LOCATION)); } // record headers size to allow HTTPSampleResult.getBytes() with different options res.setHeadersSize(responseHeaders.replaceAll("\n", "\r\n") // $NON-NLS-1$ $NON-NLS-2$ .length() + 2); // add 2 for a '\r\n' at end of headers (before data) if (log.isDebugEnabled()) { log.debug("Response headersSize=" + res.getHeadersSize() + " bodySize=" + res.getBodySize() + " Total=" + (res.getHeadersSize() + res.getBodySize())); } // If we redirected automatically, the URL may have changed if (getAutoRedirects()) { res.setURL(conn.getURL()); } // Store any cookies received in the cookie manager: saveConnectionCookies(conn, url, getCookieManager()); // Save cache information if (cacheManager != null) { cacheManager.saveDetails(conn, res); } res = resultProcessing(areFollowingRedirect, frameDepth, res); log.debug("End : sample"); return res; } catch (IOException e) { res.sampleEnd(); savedConn = null; // we don't want interrupt to try disconnection again // We don't want to continue using this connection, even if KeepAlive is set if (conn != null) { // May not exist conn.disconnect(); } conn = null; // Don't process again return errorResult(e, res); } finally { // calling disconnect doesn't close the connection immediately, // but indicates we're through with it. The JVM should close // it when necessary. savedConn = null; // we don't want interrupt to try disconnection again disconnect(conn); // Disconnect unless using KeepAlive } }
From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImplClassifier.java
/** * Samples the URL passed in and stores the result in * <code>HTTPSampleResult</code>, following redirects and downloading page * resources as appropriate./*from w w w. ja v a 2 s.co m*/ * <p> * When getting a redirect target, redirects are not followed and resources * are not downloaded. The caller will take care of this. * * @param url * URL to sample * @param method * HTTP method: GET, POST,... * @param areFollowingRedirect * whether we're getting a redirect target * @param frameDepth * Depth of this target in the frame structure. Used only to * prevent infinite recursion. * @return results of the sampling */ protected HTTPSampleResult sample(URL url, String method, boolean areFollowingRedirect, int frameDepth) { HttpURLConnection conn = null; String urlStr = url.toString(); log.debug("Start : sample " + urlStr); HTTPSampleResult res = new HTTPSampleResult(); res.setMonitor(isMonitor()); res.setSampleLabel(urlStr); res.setURL(url); res.setHTTPMethod(method); res.sampleStart(); // Count the retries as well in the time // Check cache for an entry with an Expires header in the future final CacheManager cacheManager = getCacheManager(); if (cacheManager != null && HTTPConstants.GET.equalsIgnoreCase(method)) { if (cacheManager.inCache(url)) { res.sampleEnd(); res.setResponseNoContent(); res.setSuccessful(true); return res; } } try { // Sampling proper - establish the connection and read the response: // Repeatedly try to connect: int retry; // Start with 0 so tries at least once, and retries at most // MAX_CONN_RETRIES times for (retry = 0; retry <= MAX_CONN_RETRIES; retry++) { try { conn = setupConnection(url, method, res); // Attempt the connection: savedConn = conn; conn.connect(); break; } catch (BindException e) { if (retry >= MAX_CONN_RETRIES) { log.error("Can't connect after " + retry + " retries, " + e); throw e; } log.debug("Bind exception, try again"); if (conn != null) { savedConn = null; // we don't want interrupt to try // disconnection again conn.disconnect(); } setUseKeepAlive(false); continue; // try again } catch (IOException e) { log.debug("Connection failed, giving up"); throw e; } } if (retry > MAX_CONN_RETRIES) { // This should never happen, but... throw new BindException(); } // Nice, we've got a connection. Finish sending the request: if (method.equals(HTTPConstants.POST)) { String postBody = sendPostData(conn); res.setQueryString(postBody); } else if (method.equals(HTTPConstants.PUT)) { String putBody = sendPutData(conn); res.setQueryString(putBody); } // Request sent. Now get the response: byte[] responseData = readResponse(conn, res); res.sampleEnd(); // Done with the sampling proper. // Now collect the results into the HTTPSampleResult: res.setResponseData(responseData); @SuppressWarnings("null") // Cannot be null here int errorLevel = conn.getResponseCode(); String respMsg = conn.getResponseMessage(); String hdr = conn.getHeaderField(0); if (hdr == null) { hdr = "(null)"; // $NON-NLS-1$ } if (errorLevel == -1) {// Bug 38902 - sometimes -1 seems to be // returned unnecessarily if (respMsg != null) {// Bug 41902 - NPE try { errorLevel = Integer.parseInt(respMsg.substring(0, 3)); log.warn("ResponseCode==-1; parsed " + respMsg + " as " + errorLevel); } catch (NumberFormatException e) { log.warn("ResponseCode==-1; could not parse " + respMsg + " hdr: " + hdr); } } else { respMsg = hdr; // for result log.warn("ResponseCode==-1 & null ResponseMessage. Header(0)= " + hdr); } } if (errorLevel == -1) { res.setResponseCode("(null)"); // $NON-NLS-1$ } else { res.setResponseCode(Integer.toString(errorLevel)); } res.setSuccessful(isSuccessCode(errorLevel)); if (respMsg == null) {// has been seen in a redirect respMsg = hdr; // use header (if possible) if no message found } res.setResponseMessage(respMsg); String ct = conn.getContentType(); if (ct != null) { res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1 res.setEncodingAndType(ct); } String responseHeaders = getResponseHeaders(conn); res.setResponseHeaders(responseHeaders); if (res.isRedirect()) { res.setRedirectLocation(conn.getHeaderField(HTTPConstants.HEADER_LOCATION)); } // record headers size to allow HTTPSampleResult.getBytes() with // different options res.setHeadersSize(responseHeaders.replaceAll("\n", "\r\n") // $NON-NLS-1$ // $NON-NLS-2$ .length() + 2); // add 2 for a '\r\n' at end of headers // (before data) if (log.isDebugEnabled()) { log.debug("Response headersSize=" + res.getHeadersSize() + " bodySize=" + res.getBodySize() + " Total=" + (res.getHeadersSize() + res.getBodySize())); } // If we redirected automatically, the URL may have changed if (getAutoRedirects()) { res.setURL(conn.getURL()); } // Store any cookies received in the cookie manager: saveConnectionCookies(conn, url, getCookieManager()); // Save cache information if (cacheManager != null) { cacheManager.saveDetails(conn, res); } res = resultProcessing(areFollowingRedirect, frameDepth, res); log.debug("End : sample"); return res; } catch (IOException e) { res.sampleEnd(); savedConn = null; // we don't want interrupt to try disconnection // again // We don't want to continue using this connection, even if // KeepAlive is set if (conn != null) { // May not exist conn.disconnect(); } conn = null; // Don't process again return errorResult(e, res); } finally { // calling disconnect doesn't close the connection immediately, // but indicates we're through with it. The JVM should close // it when necessary. savedConn = null; // we don't want interrupt to try disconnection // again disconnect(conn); // Disconnect unless using KeepAlive } }
From source file:org.witness.ssc.xfer.utils.PublishingUtils.java
private String uploadMetaData(final Activity activity, final Handler handler, String filePath, String title, String description, boolean retry) throws IOException { String uploadUrl = INITIAL_UPLOAD_URL; HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true);//from ww w . j a v a2 s . co m urlConnection.setRequestProperty("Content-Type", "application/atom+xml"); // urlConnection.setRequestProperty("Content-Length", newValue); urlConnection.setRequestProperty("Slug", filePath); String atomData = null; String category = DEFAULT_VIDEO_CATEGORY; this.tags = DEFAULT_VIDEO_TAGS; String template = readFile(activity, R.raw.gdata).toString(); // Workarounds for corner cases. Youtube doesnt like empty titles if (title == null || title.length() == 0) { title = "Untitled"; } if (description == null || description.length() == 0) { description = "No description"; } atomData = String.format(template, title, description, category, this.tags); OutputStreamWriter outStreamWriter = null; int responseCode = -1; try { outStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream()); outStreamWriter.write(atomData); outStreamWriter.close(); /* * urlConnection.connect(); InputStream is = * urlConnection.getInputStream(); BufferedReader in = new * BufferedReader(new InputStreamReader(is)); String inputLine; * * while ((inputLine = in.readLine()) != null) { * Log.d(TAG,inputLine); } in.close(); */ responseCode = urlConnection.getResponseCode(); // ERROR LOGGING InputStream is = urlConnection.getErrorStream(); if (is != null) { Log.e(TAG, " Error stream from Youtube available!"); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { Log.d(TAG, inputLine); } in.close(); Map<String, List<String>> hfs = urlConnection.getHeaderFields(); for (Entry<String, List<String>> hf : hfs.entrySet()) { Log.d(TAG, " entry : " + hf.getKey()); List<String> vals = hf.getValue(); for (String s : vals) { Log.d(TAG, "vals:" + s); } } } } catch (IOException e) { // // Catch IO Exceptions here, like UnknownHostException, so we can // detect network failures, and send a notification // Log.d(TAG, " Error occured in uploadMetaData! "); e.printStackTrace(); responseCode = -1; outStreamWriter = null; // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((SSCXferActivity) activity).finishedUploading(false); ((SSCXferActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); // forward it on! throw e; } if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry) { Log.d(TAG, "retrying to fetch auth token for " + youTubeName); this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken); // Try again with fresh token return uploadMetaData(activity, handler, filePath, title, description, false); } else { // Probably not authorised! // Need to setup a Youtube account. // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((SSCXferActivity) activity).finishedUploading(false); ((SSCXferActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); throw new IOException(String.format("response code='%s' (code %d)" + " for %s", urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); } } return urlConnection.getHeaderField("Location"); }
From source file:au.com.infiniterecursion.vidiom.utils.PublishingUtils.java
private String uploadMetaData(final Activity activity, final Handler handler, String filePath, String title, String description, boolean retry, long sdrecord_id) throws IOException { String uploadUrl = INITIAL_UPLOAD_URL; HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true);/* w w w.j ava 2 s. c o m*/ urlConnection.setRequestProperty("Content-Type", "application/atom+xml"); // urlConnection.setRequestProperty("Content-Length", newValue); urlConnection.setRequestProperty("Slug", filePath); String atomData = null; String category = DEFAULT_VIDEO_CATEGORY; this.tags = DEFAULT_VIDEO_TAGS; String template = readFile(activity, R.raw.gdata).toString(); // Workarounds for corner cases. Youtube doesnt like empty titles if (title == null || title.length() == 0) { title = "Untitled"; } if (description == null || description.length() == 0) { description = "No description"; } // Check user preference to see if YouTube videos should be private by // default. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity.getBaseContext()); Boolean ytPrivate = prefs.getBoolean("defaultYouTubePrivatePreference", true); String private_or_not = "<yt:private />"; if (!ytPrivate) { private_or_not = ""; } atomData = String.format(template, title, description, category, this.tags, private_or_not); OutputStreamWriter outStreamWriter = null; int responseCode = -1; try { outStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream()); outStreamWriter.write(atomData); outStreamWriter.close(); /* * urlConnection.connect(); InputStream is = * urlConnection.getInputStream(); BufferedReader in = new * BufferedReader(new InputStreamReader(is)); String inputLine; * * while ((inputLine = in.readLine()) != null) { * Log.d(TAG,inputLine); } in.close(); */ responseCode = urlConnection.getResponseCode(); // ERROR LOGGING InputStream is = urlConnection.getErrorStream(); if (is != null) { Log.e(TAG, " Error stream from Youtube available!"); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { Log.d(TAG, inputLine); } in.close(); Map<String, List<String>> hfs = urlConnection.getHeaderFields(); for (Entry<String, List<String>> hf : hfs.entrySet()) { Log.d(TAG, " entry : " + hf.getKey()); List<String> vals = hf.getValue(); for (String s : vals) { Log.d(TAG, "vals:" + s); } } } } catch (IOException e) { // // Catch IO Exceptions here, like UnknownHostException, so we can // detect network failures, and send a notification // Log.d(TAG, " Error occured in uploadMetaData! "); e.printStackTrace(); responseCode = -1; outStreamWriter = null; mainapp.removeSDFileRecordIDfromUploadingTrack(sdrecord_id, TYPE_YT); // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((VidiomActivity) activity).finishedUploading(false); ((VidiomActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); // forward it on! throw e; } if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry) { Log.d(TAG, "retrying to fetch auth token for " + youTubeName); this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken); // Try again with fresh token return uploadMetaData(activity, handler, filePath, title, description, false, sdrecord_id); } else { mainapp.removeSDFileRecordIDfromUploadingTrack(sdrecord_id, TYPE_YT); // Probably not authorised! // Need to setup a Youtube account. // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((VidiomActivity) activity).finishedUploading(false); ((VidiomActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); throw new IOException(String.format("response code='%s' (code %d)" + " for %s", urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); } } return urlConnection.getHeaderField("Location"); }