List of usage examples for java.net URLConnection setUseCaches
public void setUseCaches(boolean usecaches)
From source file:com.dragonflow.StandardMonitor.URLOriginalMonitor.java
private static long getURLStatus_ForBackupToRegularMeansOnly(SocketSession socketsession, long l, String s, int i, int j) { if (i >= j) { System.out.println("URLOriginalMonitor KEEPTRYING!!! - URL: " + s + "FAILED THIS MANY TIMES: " + i); return l; }//from w ww .j av a 2s.c om if (l != 200L && l != 302L && l != 301L) { System.out.println( "URLOriginalMonitor KEEPTRYING!!! - URL: " + s + " status: " + l + " times attempted: " + i); int k = 0; if (socketsession.context.getSetting("_timeout").length() > 0) { k = TextUtils.toInt(socketsession.context.getSetting("_timeout")) * 1000; } else { k = 60000; } Platform.sleep(k); StringBuffer stringbuffer = new StringBuffer(); try { URL url = new URL(s); URLConnection urlconnection = url.openConnection(); System.out.println( "URLOriginalMonitor KEEPTRYING Received a : " + urlconnection.getClass().getName()); System.out.println( "URLOriginalMonitor KEEPTRYING Received connection string : " + urlconnection.toString()); urlconnection.setDoInput(true); urlconnection.setUseCaches(false); System.out.println("URLOriginalMonitor KEEPTRYING Getting an input stream..."); java.io.InputStream inputstream = urlconnection.getInputStream(); InputStreamReader inputstreamreader = new InputStreamReader(inputstream); BufferedReader bufferedreader = new BufferedReader(inputstreamreader); for (String s5 = null; (s5 = bufferedreader.readLine()) != null;) { stringbuffer.append(s5); } System.out.println("URLOriginalMonitor KEEPTRYING CONTENTS: " + stringbuffer.toString()); } catch (MalformedURLException malformedurlexception) { String s1 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s + " exception: " + malformedurlexception.toString(); LogManager.log("Error", s1); LogManager.log("Error", FileUtils.stackTraceText(malformedurlexception)); System.out.println(s1); malformedurlexception.printStackTrace(); return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j); } catch (IOException ioexception) { String s2 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s + " exception: " + ioexception.toString(); LogManager.log("Error", s2); LogManager.log("Error", FileUtils.stackTraceText(ioexception)); System.out.println(s2); ioexception.printStackTrace(); return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j); } catch (Exception exception) { String s3 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s + " exception: " + exception.toString(); LogManager.log("Error", s3); LogManager.log("Error", FileUtils.stackTraceText(exception)); System.out.println(s3); exception.printStackTrace(); return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j); } catch (Throwable throwable) { String s4 = "URLOriginalMonitor KEEPTRYING error attempted backup URL retrieval: " + s + " exception: " + throwable.toString(); LogManager.log("Error", s4); LogManager.log("Error", FileUtils.stackTraceText(throwable)); System.out.println(s4); throwable.printStackTrace(); return getURLStatus_ForBackupToRegularMeansOnly(socketsession, l, s, ++i, j); } return 200L; } else { return l; } }
From source file:ch.unifr.pai.twice.widgets.mpproxy.server.JettyProxy.java
public ProcessResult loadFromProxy(HttpServletRequest request, HttpServletResponse response, String uri, String servletPath, String proxyPath) throws ServletException, IOException { //System.out.println("LOAD "+uri); //System.out.println("LOAD "+proxyPath); if ("CONNECT".equalsIgnoreCase(request.getMethod())) { handleConnect(request, response); } else {/*from www.j a va 2 s. com*/ URL url = new URL(uri); URLConnection connection = url.openConnection(); connection.setAllowUserInteraction(false); // Set method HttpURLConnection http = null; if (connection instanceof HttpURLConnection) { http = (HttpURLConnection) connection; http.setRequestMethod(request.getMethod()); http.setInstanceFollowRedirects(false); } // check connection header String connectionHdr = request.getHeader("Connection"); if (connectionHdr != null) { connectionHdr = connectionHdr.toLowerCase(); if (connectionHdr.equals("keep-alive") || connectionHdr.equals("close")) connectionHdr = null; } // copy headers boolean xForwardedFor = false; boolean hasContent = false; Enumeration enm = request.getHeaderNames(); while (enm.hasMoreElements()) { // TODO could be better than this! String hdr = (String) enm.nextElement(); String lhdr = hdr.toLowerCase(); if (_DontProxyHeaders.contains(lhdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(lhdr) >= 0) continue; if ("content-type".equals(lhdr)) hasContent = true; Enumeration vals = request.getHeaders(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { connection.addRequestProperty(hdr, val); xForwardedFor |= "X-Forwarded-For".equalsIgnoreCase(hdr); } } } // Proxy headers connection.setRequestProperty("Via", "1.1 (jetty)"); if (!xForwardedFor) connection.addRequestProperty("X-Forwarded-For", request.getRemoteAddr()); // a little bit of cache control String cache_control = request.getHeader("Cache-Control"); if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) connection.setUseCaches(false); // customize Connection try { connection.setDoInput(true); // do input thang! InputStream in = request.getInputStream(); if (hasContent) { connection.setDoOutput(true); IOUtils.copy(in, connection.getOutputStream()); } // Connect connection.connect(); } catch (Exception e) { e.printStackTrace(); } InputStream proxy_in = null; // handler status codes etc. int code = 500; if (http != null) { proxy_in = http.getErrorStream(); code = http.getResponseCode(); response.setStatus(code, http.getResponseMessage()); } if (proxy_in == null) { try { proxy_in = connection.getInputStream(); } catch (Exception e) { e.printStackTrace(); proxy_in = http.getErrorStream(); } } // clear response defaults. response.setHeader("Date", null); response.setHeader("Server", null); // set response headers int h = 0; String hdr = connection.getHeaderFieldKey(h); String val = connection.getHeaderField(h); while (hdr != null || val != null) { String lhdr = hdr != null ? hdr.toLowerCase() : null; if (hdr != null && val != null && !_DontProxyHeaders.contains(lhdr)) { if (hdr.equalsIgnoreCase("Location")) { val = Rewriter.translateCleanUrl(val, servletPath, proxyPath); } response.addHeader(hdr, val); } h++; hdr = connection.getHeaderFieldKey(h); val = connection.getHeaderField(h); } boolean isGzipped = connection.getContentEncoding() != null && connection.getContentEncoding().contains("gzip"); response.addHeader("Via", "1.1 (jetty)"); // boolean process = connection.getContentType() == null // || connection.getContentType().isEmpty() // || connection.getContentType().contains("html"); boolean process = connection.getContentType() != null && connection.getContentType().contains("text"); if (proxy_in != null) { if (!process) { IOUtils.copy(proxy_in, response.getOutputStream()); proxy_in.close(); } else { InputStream in; if (isGzipped && proxy_in != null && proxy_in.available() > 0) { in = new GZIPInputStream(proxy_in); } else { in = proxy_in; } ByteArrayOutputStream byteArrOS = new ByteArrayOutputStream(); IOUtils.copy(in, byteArrOS); in.close(); if (in != proxy_in) proxy_in.close(); String charset = response.getCharacterEncoding(); if (charset == null || charset.isEmpty()) { charset = "ISO-8859-1"; } String originalContent = new String(byteArrOS.toByteArray(), charset); byteArrOS.close(); return new ProcessResult(originalContent, connection.getContentType(), charset, isGzipped); } } } return null; }
From source file:com.adobe.aem.demomachine.communities.Loader.java
private static void postAnalytics(String analytics, String body) { if (analytics != null && body != null) { URLConnection urlConn = null; DataOutputStream printout = null; BufferedReader input = null; String tmp = null;//ww w .j av a2s .c o m try { logger.debug("New Analytics Event: " + body); URL sitecaturl = new URL("http://" + analytics); urlConn = sitecaturl.openConnection(); urlConn.setDoInput(true); urlConn.setDoOutput(true); urlConn.setUseCaches(false); urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); printout = new DataOutputStream(urlConn.getOutputStream()); printout.writeBytes(body); printout.flush(); printout.close(); input = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); while (null != ((tmp = input.readLine()))) { logger.debug(tmp); } printout.close(); input.close(); } catch (Exception ex) { logger.warn("Connectivity error: " + ex.getMessage()); } finally { try { input.close(); printout.close(); } catch (Exception e) { // Omitted } } } }
From source file:net.lightbody.bmp.proxy.jetty.http.handler.ProxyHandler.java
public void handle(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws HttpException, IOException { URI uri = request.getURI();/*from w ww . j a v a 2 s . c om*/ // Is this a CONNECT request? if (HttpRequest.__CONNECT.equalsIgnoreCase(request.getMethod())) { response.setField(HttpFields.__Connection, "close"); // TODO Needed for IE???? handleConnect(pathInContext, pathParams, request, response); return; } try { // Do we proxy this? URL url = isProxied(uri); if (url == null) { if (isForbidden(uri)) sendForbid(request, response, uri); return; } if (log.isDebugEnabled()) log.debug("PROXY URL=" + url); URLConnection connection = url.openConnection(); connection.setAllowUserInteraction(false); // Set method HttpURLConnection http = null; if (connection instanceof HttpURLConnection) { http = (HttpURLConnection) connection; http.setRequestMethod(request.getMethod()); http.setInstanceFollowRedirects(false); } // check connection header String connectionHdr = request.getField(HttpFields.__Connection); if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive) || connectionHdr.equalsIgnoreCase(HttpFields.__Close))) connectionHdr = null; // copy headers boolean xForwardedFor = false; boolean hasContent = false; Enumeration enm = request.getFieldNames(); while (enm.hasMoreElements()) { // TODO could be better than this! String hdr = (String) enm.nextElement(); if (_DontProxyHeaders.containsKey(hdr) || !_chained && _ProxyAuthHeaders.containsKey(hdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0) continue; if (HttpFields.__ContentType.equals(hdr)) hasContent = true; Enumeration vals = request.getFieldValues(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { connection.addRequestProperty(hdr, val); xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr); } } } // Proxy headers if (!_anonymous) connection.setRequestProperty("Via", "1.1 (jetty)"); if (!xForwardedFor) connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr()); // a little bit of cache control String cache_control = request.getField(HttpFields.__CacheControl); if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) connection.setUseCaches(false); // customize Connection customizeConnection(pathInContext, pathParams, request, connection); try { connection.setDoInput(true); // do input thang! InputStream in = request.getInputStream(); if (hasContent) { connection.setDoOutput(true); IO.copy(in, connection.getOutputStream()); } // Connect connection.connect(); } catch (Exception e) { LogSupport.ignore(log, e); } InputStream proxy_in = null; // handler status codes etc. int code = HttpResponse.__500_Internal_Server_Error; if (http != null) { proxy_in = http.getErrorStream(); code = http.getResponseCode(); response.setStatus(code); response.setReason(http.getResponseMessage()); } if (proxy_in == null) { try { proxy_in = connection.getInputStream(); } catch (Exception e) { LogSupport.ignore(log, e); proxy_in = http.getErrorStream(); } } // clear response defaults. response.removeField(HttpFields.__Date); response.removeField(HttpFields.__Server); // set response headers int h = 0; String hdr = connection.getHeaderFieldKey(h); String val = connection.getHeaderField(h); while (hdr != null || val != null) { if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr) && (_chained || !_ProxyAuthHeaders.containsKey(hdr))) response.addField(hdr, val); h++; hdr = connection.getHeaderFieldKey(h); val = connection.getHeaderField(h); } if (!_anonymous) response.setField("Via", "1.1 (jetty)"); // Handled request.setHandled(true); if (proxy_in != null) IO.copy(proxy_in, response.getOutputStream()); } catch (Exception e) { log.warn(e.toString()); LogSupport.ignore(log, e); if (!response.isCommitted()) response.sendError(HttpResponse.__400_Bad_Request); } }
From source file:com.jp.miaulavirtual.DisplayMessageActivity.java
public int downloadFile(String request) { URL url2;/* w w w. ja va 2 s.c om*/ URLConnection conn; int lastSlash; Long fileSize = null; BufferedInputStream inStream; BufferedOutputStream outStream; FileOutputStream fileStream; String cookies = cookieFormat(scookie); // format cookie for URL setRequestProperty final int BUFFER_SIZE = 23 * 1024; int id = 1; File file = null; Log.d("Document", "2 respueesta"); try { // Just resources lastSlash = url.toString().lastIndexOf('/'); // Directory creation String root = Environment.getExternalStorageDirectory().toString(); Boolean isSDPresent = android.os.Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); // check if is there external storage if (!isSDPresent) { task_status = false; id = 9; } else { String folder; if (comunidades) { folder = onData.get(2)[1]; } else { folder = onData.get(1)[1]; } folder = folder.replaceAll( "\\d{4}-\\d{4}\\s|\\d{4}-\\d{2}\\s|Documentos\\sde\\s?|Gr\\..+?\\s|\\(.+?\\)", ""); folder = folder.toString().trim(); Log.d("Folder", folder); File myDir = new File(root + "/Android/data/com.jp.miaulavirtual/files/" + folder); myDir.mkdirs(); // Document creation String name = url.toString().substring(lastSlash + 1); file = new File(myDir, name); Log.d("Document", name); fileSize = (long) file.length(); // Check if we have already downloaded the whole file if (file.exists()) { dialog.setProgress(100); // full progress if file already donwloaded } else { // Start the connection with COOKIES (we already verified that the cookies aren't expired and we can use them) url2 = new URL(request); conn = url2.openConnection(); conn.setUseCaches(false); conn.setRequestProperty("Cookie", cookies); conn.setConnectTimeout(10 * 1000); conn.setReadTimeout(20 * 1000); fileSize = (long) conn.getContentLength(); // Check if we have necesary space if (fileSize >= myDir.getUsableSpace()) { task_status = false; id = 2; } else { // Start downloading inStream = new BufferedInputStream(conn.getInputStream()); fileStream = new FileOutputStream(file); outStream = new BufferedOutputStream(fileStream, BUFFER_SIZE); byte[] data = new byte[BUFFER_SIZE]; int bytesRead = 0; int setMax = (conn.getContentLength() / 1024); dialog.setMax(setMax); while (task_status && (bytesRead = inStream.read(data, 0, data.length)) >= 0) { outStream.write(data, 0, bytesRead); // update progress bar dialog.incrementProgressBy((int) (bytesRead / 1024)); } // Close stream outStream.close(); fileStream.close(); inStream.close(); // Delete file if Cancel button if (!task_status) { file.delete(); if (myDir.listFiles().length <= 0) myDir.delete(); id = 0; } } } Log.d("Status", String.valueOf(task_status)); // Open file if (task_status) { Log.d("Type", "Hola2"); dialog.dismiss(); Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); MimeTypeMap mime = MimeTypeMap.getSingleton(); // Get extension file String file_s = file.toString(); String extension = ""; int i = file_s.lastIndexOf('.'); int p = Math.max(file_s.lastIndexOf('/'), file_s.lastIndexOf('\\')); if (i > p) { extension = file_s.substring(i + 1); } // Get extension reference String doc_type = mime.getMimeTypeFromExtension(extension); Log.d("Type", extension); intent.setDataAndType(Uri.fromFile(file), doc_type); startActivity(intent); } } } catch (MalformedURLException e) // Invalid URL { task_status = false; if (file.exists()) { file.delete(); } id = 3; } catch (FileNotFoundException e) // FIle not found { task_status = false; if (file.exists()) { file.delete(); } id = 4; } catch (SocketTimeoutException e) // time out { Log.d("Timeout", "Timeout"); task_status = false; if (file.exists()) { file.delete(); } id = 7; } catch (Exception e) // General error { task_status = false; if (file.exists()) { file.delete(); } id = 8; } Log.d("Type", String.valueOf(id)); Log.d("StartOk3", "Como he llegado hasta aqu?"); // notify completion Log.d("ID", String.valueOf(id)); return id; }
From source file:org.lockss.proxy.ProxyHandler.java
/** Proxy a connection using Java's native URLConection */ void doSun(String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws IOException { URI uri = request.getURI();//from w ww . j a v a2s. c o m try { // Do we proxy this? URL url = isProxied(uri); if (url == null) { if (isForbidden(uri)) { sendForbid(request, response, uri); logAccess(request, "forbidden method: " + request.getMethod()); } return; } if (jlog.isDebugEnabled()) jlog.debug("PROXY URL=" + url); URLConnection connection = url.openConnection(); connection.setAllowUserInteraction(false); // Set method HttpURLConnection http = null; if (connection instanceof HttpURLConnection) { http = (HttpURLConnection) connection; http.setRequestMethod(request.getMethod()); http.setInstanceFollowRedirects(false); } // check connection header String connectionHdr = request.getField(HttpFields.__Connection); if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive) || connectionHdr.equalsIgnoreCase(HttpFields.__Close))) connectionHdr = null; // copy headers boolean hasContent = false; Enumeration en = request.getFieldNames(); while (en.hasMoreElements()) { // XXX could be better than this! String hdr = (String) en.nextElement(); if (_DontProxyHeaders.containsKey(hdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0) continue; if (HttpFields.__ContentType.equalsIgnoreCase(hdr)) hasContent = true; Enumeration vals = request.getFieldValues(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { connection.addRequestProperty(hdr, val); } } } // Proxy headers connection.addRequestProperty(HttpFields.__Via, makeVia(request)); connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr()); // a little bit of cache control String cache_control = request.getField(HttpFields.__CacheControl); if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) connection.setUseCaches(false); // customize Connection customizeConnection(pathInContext, pathParams, request, connection); try { connection.setDoInput(true); // do input thang! InputStream in = request.getInputStream(); if (hasContent) { connection.setDoOutput(true); IO.copy(in, connection.getOutputStream()); } // Connect connection.connect(); } catch (Exception e) { LogSupport.ignore(jlog, e); } InputStream proxy_in = null; // handler status codes etc. int code = HttpResponse.__500_Internal_Server_Error; if (http != null) { proxy_in = http.getErrorStream(); code = http.getResponseCode(); response.setStatus(code); response.setReason(http.getResponseMessage()); } if (proxy_in == null) { try { proxy_in = connection.getInputStream(); } catch (Exception e) { LogSupport.ignore(jlog, e); proxy_in = http.getErrorStream(); } } // clear response defaults. response.removeField(HttpFields.__Date); response.removeField(HttpFields.__Server); // set response headers int h = 0; String hdr = connection.getHeaderFieldKey(h); String val = connection.getHeaderField(h); while (hdr != null || val != null) { if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr)) response.addField(hdr, val); h++; hdr = connection.getHeaderFieldKey(h); val = connection.getHeaderField(h); } response.addField(HttpFields.__Via, makeVia(request)); // Handled request.setHandled(true); if (proxy_in != null) IO.copy(proxy_in, response.getOutputStream()); } catch (Exception e) { log.warning("doSun error", e); if (!response.isCommitted()) response.sendError(HttpResponse.__400_Bad_Request, e.getMessage()); } }
From source file:org.openqa.selenium.server.ProxyHandler.java
protected long proxyPlainTextRequest(URL url, String pathInContext, String pathParams, HttpRequest request, HttpResponse response) throws IOException { CaptureNetworkTrafficCommand.Entry entry = new CaptureNetworkTrafficCommand.Entry(request.getMethod(), url.toString());/*from ww w. j a v a 2 s. c o m*/ entry.addRequestHeaders(request); if (log.isDebugEnabled()) log.debug("PROXY URL=" + url); URLConnection connection = url.openConnection(); connection.setAllowUserInteraction(false); if (proxyInjectionMode) { adjustRequestForProxyInjection(request, connection); } // Set method HttpURLConnection http = null; if (connection instanceof HttpURLConnection) { http = (HttpURLConnection) connection; http.setRequestMethod(request.getMethod()); http.setInstanceFollowRedirects(false); if (trustAllSSLCertificates && connection instanceof HttpsURLConnection) { TrustEverythingSSLTrustManager.trustAllSSLCertificates((HttpsURLConnection) connection); } } // check connection header String connectionHdr = request.getField(HttpFields.__Connection); if (connectionHdr != null && (connectionHdr.equalsIgnoreCase(HttpFields.__KeepAlive) || connectionHdr.equalsIgnoreCase(HttpFields.__Close))) connectionHdr = null; // copy headers boolean xForwardedFor = false; boolean isGet = "GET".equals(request.getMethod()); boolean hasContent = false; Enumeration enm = request.getFieldNames(); while (enm.hasMoreElements()) { // TODO could be better than this! String hdr = (String) enm.nextElement(); if (_DontProxyHeaders.containsKey(hdr) || !_chained && _ProxyAuthHeaders.containsKey(hdr)) continue; if (connectionHdr != null && connectionHdr.indexOf(hdr) >= 0) continue; if (!isGet && HttpFields.__ContentType.equals(hdr)) hasContent = true; Enumeration vals = request.getFieldValues(hdr); while (vals.hasMoreElements()) { String val = (String) vals.nextElement(); if (val != null) { // don't proxy Referer headers if the referer is Selenium! if ("Referer".equals(hdr) && (-1 != val.indexOf("/selenium-server/"))) { continue; } if (!isGet && HttpFields.__ContentLength.equals(hdr) && Integer.parseInt(val) > 0) { hasContent = true; } connection.addRequestProperty(hdr, val); xForwardedFor |= HttpFields.__XForwardedFor.equalsIgnoreCase(hdr); } } } // add any custom request headers that the user asked for Map<String, String> customRequestHeaders = AddCustomRequestHeaderCommand.getHeaders(); for (Map.Entry<String, String> e : customRequestHeaders.entrySet()) { connection.addRequestProperty(e.getKey(), e.getValue()); entry.addRequestHeader(e.getKey(), e.getValue()); } // Proxy headers if (!_anonymous) connection.setRequestProperty("Via", "1.1 (jetty)"); if (!xForwardedFor) connection.addRequestProperty(HttpFields.__XForwardedFor, request.getRemoteAddr()); // a little bit of cache control String cache_control = request.getField(HttpFields.__CacheControl); if (cache_control != null && (cache_control.indexOf("no-cache") >= 0 || cache_control.indexOf("no-store") >= 0)) connection.setUseCaches(false); // customize Connection customizeConnection(pathInContext, pathParams, request, connection); try { connection.setDoInput(true); // do input thang! InputStream in = request.getInputStream(); if (hasContent) { connection.setDoOutput(true); IO.copy(in, connection.getOutputStream()); } // Connect connection.connect(); } catch (Exception e) { LogSupport.ignore(log, e); } InputStream proxy_in = null; // handler status codes etc. int code = -1; if (http != null) { proxy_in = http.getErrorStream(); try { code = http.getResponseCode(); } catch (SSLHandshakeException e) { throw new RuntimeException("Couldn't establish SSL handshake. Try using trustAllSSLCertificates.\n" + e.getLocalizedMessage(), e); } response.setStatus(code); response.setReason(http.getResponseMessage()); String contentType = http.getContentType(); if (log.isDebugEnabled()) { log.debug("Content-Type is: " + contentType); } } if (proxy_in == null) { try { proxy_in = connection.getInputStream(); } catch (Exception e) { LogSupport.ignore(log, e); proxy_in = http.getErrorStream(); } } // clear response defaults. response.removeField(HttpFields.__Date); response.removeField(HttpFields.__Server); // set response headers int h = 0; String hdr = connection.getHeaderFieldKey(h); String val = connection.getHeaderField(h); while (hdr != null || val != null) { if (hdr != null && val != null && !_DontProxyHeaders.containsKey(hdr) && (_chained || !_ProxyAuthHeaders.containsKey(hdr))) response.addField(hdr, val); h++; hdr = connection.getHeaderFieldKey(h); val = connection.getHeaderField(h); } if (!_anonymous) response.setField("Via", "1.1 (jetty)"); response.removeField(HttpFields.__ETag); // possible cksum? Stop caching... response.removeField(HttpFields.__LastModified); // Stop caching... // Handled long bytesCopied = -1; request.setHandled(true); if (proxy_in != null) { boolean injectableResponse = http.getResponseCode() == HttpURLConnection.HTTP_OK || (http.getResponseCode() >= 400 && http.getResponseCode() < 600); if (proxyInjectionMode && injectableResponse) { // check if we should proxy this path based on the dontProxyRegex that can be user-specified if (shouldInject(request.getPath())) { bytesCopied = InjectionHelper.injectJavaScript(request, response, proxy_in, response.getOutputStream(), debugURL); } else { bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream()); } } else { bytesCopied = ModifiedIO.copy(proxy_in, response.getOutputStream()); } } entry.finish(code, bytesCopied); entry.addResponseHeader(response); CaptureNetworkTrafficCommand.capture(entry); return bytesCopied; }
From source file:com.icesoft.faces.webapp.parser.JspPageToDocument.java
/** * @param context//w ww. j av a 2 s. c o m * @param namespaceURL * @return InputStream * @throws IOException */ public static InputStream getTldInputStream(ExternalContext context, String namespaceURL) throws IOException { // InputStream in = null; JarFile jarFile = null; String[] location = null; namespaceURL = String.valueOf(namespaceURL); // "jsp" is only a placeholder for standard JSP tags that are // not supported, so just return null if ("jsp".equals(namespaceURL)) { return null; } if ("http://java.sun.com/JSP/Page".equals(namespaceURL)) { return null; } // TldLocationsCache may fail esp. with SecurityException on SUN app server TldLocationsCache tldCache = new TldLocationsCache(context); try { location = tldCache.getLocation(namespaceURL); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } if (null == location) { if (namespaceURL.startsWith("/") && namespaceURL.endsWith(".tld")) { location = new String[] { namespaceURL }; } } if (null == location) { location = scanJars(context, namespaceURL); } if (null == location) { //look for Sun implementation URL tagURL = JspPageToDocument.class.getClassLoader().getResource(SUN_TAG_CLASS); if (null != tagURL) { // Bug 876 // Not all app servers (ie WebLogic 8.1) return // an actual JarURLConnection. URLConnection conn = tagURL.openConnection(); //ICE-3683: Special processing for JBoss 5 micro-container with VFS if (tagURL.getProtocol().equals("vfszip")) { String tagPath = tagURL.toExternalForm(); String jarPath = tagPath.substring(0, tagPath.indexOf(SUN_TAG_CLASS)); String tldPath = jarPath; if (namespaceURL.endsWith("html")) { tldPath += HTML_TLD_SUFFIX; } else if (namespaceURL.endsWith("core")) { tldPath += CORE_TLD_SUFFIX; } URL tldURL = new URL(tldPath); return tldURL.openConnection().getInputStream(); } if (conn instanceof JarURLConnection) { location = scanJar((JarURLConnection) conn, namespaceURL); } else { //OSGi-based servers (such as GlassFishv3 and WebSphere7) //do not provide JarURLConnection to their resources so //we handle the JSF TLDs as a special case. if (namespaceURL.endsWith("html")) { location = getBundleLocation(tagURL, HTML_TLD_SUFFIX); } else if (namespaceURL.endsWith("core")) { location = getBundleLocation(tagURL, CORE_TLD_SUFFIX); } } } } if (null == location) { try { // scan WebSphere dirs for JSF jars String separator = System.getProperty("path.separator"); String wsDirs = System.getProperty("ws.ext.dirs"); String[] dirs = null; if (null != wsDirs) { dirs = wsDirs.split(separator); } else { dirs = new String[] {}; } Iterator theDirs = Arrays.asList(dirs).iterator(); while (theDirs.hasNext()) { String dir = (String) theDirs.next(); try { location = scanJars(dir, namespaceURL); } catch (Exception e) { //catch all possible exceptions including runtime exception //so that the rest of jars still can be scanned. } if (null != location) { break; } } } catch (Exception e) { if (log.isDebugEnabled()) { log.debug(e.getMessage(), e); } } } if (null == location) { //look for MyFaces implementation URL tagURL = JspPageToDocument.class.getClassLoader().getResource(MYFACES_TAG_CLASS); if (null != tagURL) { URLConnection conn = tagURL.openConnection(); if (conn instanceof JarURLConnection) { location = scanJar((JarURLConnection) conn, namespaceURL); } else { //OSGi-based servers (such as GlassFishv3 and WebSphere7) //do not provide JarURLConnection to their resources so //we handle the JSF TLDs as a special case. if (namespaceURL.endsWith("html")) { location = getBundleLocation(tagURL, HTML_TLD_SUFFIX); } else if (namespaceURL.endsWith("core")) { location = getBundleLocation(tagURL, CORE_TLD_SUFFIX); } } } } if (null == location) { String msg = "Can't find TLD for location [" + namespaceURL + "]. JAR containing the TLD may not be in the classpath"; log.error(msg); return null; } else { if (log.isTraceEnabled()) { for (int i = 0; i < location.length; i++) { log.trace("Found TLD location for " + namespaceURL + " = " + location[i]); } } } if (!location[0].endsWith("jar")) { InputStream tldStream = context.getResourceAsStream(location[0]); if (null == tldStream) { tldStream = (new URL(location[0])).openConnection().getInputStream(); } return tldStream; } else { // Tag library is packaged in JAR file URL jarFileUrl = new URL("jar:" + location[0] + "!/"); JarURLConnection conn = (JarURLConnection) jarFileUrl.openConnection(); conn.setUseCaches(false); conn.connect(); jarFile = conn.getJarFile(); ZipEntry jarEntry = jarFile.getEntry(location[1]); return jarFile.getInputStream(jarEntry); } }
From source file:org.jab.docsearch.DocSearch.java
private boolean downloadURLToFile(String urlString, String fileToSaveAs) { int numBytes = 0; int curI = 0; FileOutputStream dos = null;//from w w w . jav a2s . c o m InputStream urlStream = null; int lastPercent = 0; int curPercent = 0; try { URL url = new URL(urlString); File saveFile = new File(fileToSaveAs); dos = new FileOutputStream(saveFile); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); conn.connect(); urlStream = conn.getInputStream(); int totalSize = conn.getContentLength(); while (curI != -1) { curI = urlStream.read(); byte curBint = (byte) curI; if (curI == -1) { break; } dos.write(curBint); numBytes++; if (totalSize > 0) { curPercent = (numBytes * 100) / totalSize; if (curPercent != lastPercent) { setStatus(curPercent + " " + I18n.getString("percent_downloaded") + " " + I18n.getString("of_file") + " " + urlString + " " + I18n.getString("total_bytes") + " " + totalSize); lastPercent = curPercent; } } // end if total size not zero } urlStream.close(); dos.close(); } catch (IOException ioe) { logger.fatal("downloadURLToFile() failed", ioe); showMessage(I18n.getString("error_download_file"), ioe.toString()); return false; } finally { IOUtils.closeQuietly(dos); IOUtils.closeQuietly(urlStream); } return true; }