public void doProcess(HttpServletRequest req, HttpServletResponse res, boolean isPost) { StringBuffer bodyContent = null; OutputStream out = null; PrintWriter writer = null; String serviceKey = null; try { BufferedReader in = req.getReader(); String line = null; while ((line = in.readLine()) != null) { if (bodyContent == null) bodyContent = new StringBuffer(); bodyContent.append(line); } } catch (Exception e) { } try { if (requireSession) { // check to see if there was a session created for this request // if not assume it was from another domain and blow up // Wrap this to prevent Portlet exeptions HttpSession session = req.getSession(false); if (session == null) { res.setStatus(HttpServletResponse.SC_FORBIDDEN); return; } } serviceKey = req.getParameter("id"); // only to preven regressions - Remove before 1.0 if (serviceKey == null) serviceKey = req.getParameter("key"); // check if the services have been loaded or if they need to be reloaded if (services == null || configUpdated()) { getServices(res); } String urlString = null; String xslURLString = null; String userName = null; String password = null; String format = "json"; String callback = req.getParameter("callback"); String urlParams = req.getParameter("urlparams"); String countString = req.getParameter("count"); // encode the url to prevent spaces from being passed along if (urlParams != null) { urlParams = urlParams.replace(' ', '+'); } try { if (services.has(serviceKey)) { JSONObject service = services.getJSONObject(serviceKey); // default to the service default if no url parameters are specified if (urlParams == null && service.has("defaultURLParams")) { urlParams = service.getString("defaultURLParams"); } String serviceURL = service.getString("url"); // build the URL if (urlParams != null && serviceURL.indexOf("?") == -1) { serviceURL += "?"; } else if (urlParams != null) { serviceURL += "&"; } String apikey = ""; if (service.has("username")) userName = service.getString("username"); if (service.has("password")) password = service.getString("password"); if (service.has("apikey")) apikey = service.getString("apikey"); urlString = serviceURL + apikey; if (urlParams != null) urlString += "&" + urlParams; if (service.has("xslStyleSheet")) { xslURLString = service.getString("xslStyleSheet"); } } // code for passing the url directly through instead of using configuration file else if (req.getParameter("url") != null) { String serviceURL = req.getParameter("url"); // build the URL if (urlParams != null && serviceURL.indexOf("?") == -1) { serviceURL += "?"; } else if (urlParams != null) { serviceURL += "&"; } urlString = serviceURL; if (urlParams != null) urlString += urlParams; } else { writer = res.getWriter(); if (serviceKey == null) writer.write("XmlHttpProxyServlet Error: id parameter specifying serivce required."); else writer.write( "XmlHttpProxyServlet Error : service for id '" + serviceKey + "' not found."); writer.flush(); return; } } catch (Exception ex) { getLogger().severe("XmlHttpProxyServlet Error loading service: " + ex); } Map paramsMap = new HashMap(); paramsMap.put("format", format); // do not allow for xdomain unless the context level setting is enabled. if (callback != null && allowXDomain) { paramsMap.put("callback", callback); } if (countString != null) { paramsMap.put("count", countString); } InputStream xslInputStream = null; if (urlString == null) { writer = res.getWriter(); writer.write( "XmlHttpProxyServlet parameters: id[Required] urlparams[Optional] format[Optional] callback[Optional]"); writer.flush(); return; } // default to JSON res.setContentType(responseContentType); out = res.getOutputStream(); // get the stream for the xsl stylesheet if (xslURLString != null) { // check the web root for the resource URL xslURL = null; xslURL = ctx.getResource(resourcesDir + "xsl/" + xslURLString); // if not in the web root check the classpath if (xslURL == null) { xslURL = XmlHttpProxyServlet.class.getResource(classpathResourcesDir + "xsl/" + xslURLString); } if (xslURL != null) { xslInputStream = xslURL.openStream(); } else { String message = "Could not locate the XSL stylesheet provided for service id " + serviceKey + ". Please check the XMLHttpProxy configuration."; getLogger().severe(message); try { out.write(message.getBytes()); out.flush(); return; } catch (java.io.IOException iox) { } } } if (!isPost) { xhp.doGet(urlString, out, xslInputStream, paramsMap, userName, password); } else { if (bodyContent == null) getLogger() .info( "XmlHttpProxyServlet attempting to post to url " + urlString + " with no body content"); xhp.doPost( urlString, out, xslInputStream, paramsMap, bodyContent.toString(), req.getContentType(), userName, password); } } catch (Exception iox) { iox.printStackTrace(); getLogger().severe("XmlHttpProxyServlet: caught " + iox); try { writer = res.getWriter(); writer.write(iox.toString()); writer.flush(); } catch (java.io.IOException ix) { ix.printStackTrace(); } return; } finally { try { if (out != null) out.close(); if (writer != null) writer.close(); } catch (java.io.IOException iox) { } } }
protected Boolean doInBackground(ArrayList... params) { download_photos.this.runOnUiThread( new Runnable() { public void run() { mtext.setText( getText(R.string.download_textview_message_1) + " " + path.toString() + ". "); } }); if (resume_file.exists()) { initial_value = readProgress()[0]; } else { initial_value = 1; } for (int i = initial_value - 1; i < links.size(); i++) { // asynctask expects more than one ArrayList<String> item, but we are sending only one, // which is params[0] Uri imageuri = Uri.parse(params[0].get(i).toString()); URL imageurl = null; HttpURLConnection connection = null; total_files_to_download = links.size(); completed_downloads = i + 1; try { imageurl = new URL(params[0].get(i).toString()); connection = (HttpURLConnection) imageurl.openConnection(); connection.connect(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // extracts the real file name of the photo from url path_segments = imageuri.getPathSegments(); int total_segments = path_segments.size(); file_name = path_segments.get(total_segments - 1); path.mkdirs(); // if(i==0) // first_image = path.toString() + "/" + file_name; InputStream input; OutputStream output; try { input = new BufferedInputStream(imageurl.openStream()); fully_qualified_file_name = new File(path, file_name); output = new BufferedOutputStream(new FileOutputStream(fully_qualified_file_name)); byte data[] = new byte[1024]; int count; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } output.flush(); output.close(); input.close(); connection.disconnect(); new folder_scanner(getApplicationContext(), fully_qualified_file_name); publishProgress(completed_downloads, total_files_to_download); if (this.isCancelled()) { writeProgress(completed_downloads, total_files_to_download); break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // creates required folders and subfolders if they do not exist already // boolean success = path.mkdirs(); // makes request to download photos // DownloadManager.Request request = new DownloadManager.Request(imageuri); // set path for downloads // request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,sub_path); // request.setDescription("Downloaded using Facebook Album Downloader"); // DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); // download is enqueue in download list. it returns unique id for each download // download_id = dm.enqueue(request); } // returns the unique id. we are not using this id return true; }