List of usage examples for java.net HttpURLConnection HTTP_NOT_MODIFIED
int HTTP_NOT_MODIFIED
To view the source code for java.net HttpURLConnection HTTP_NOT_MODIFIED.
Click Source Link
From source file:org.jumpmind.symmetric.transport.http.HttpIncomingTransport.java
/** * This method support redirection from an http connection to an https connection. * See {@link http://java.sun.com/j2se/1.4.2/docs/guide/deployment/deployment-guide/upgrade-guide/article-17.html} * for more information.//from ww w . ja v a2 s.c om * * @param connection * @return * @throws IOException */ private HttpURLConnection openConnectionCheckRedirects(HttpURLConnection connection) throws IOException { boolean redir; int redirects = 0; do { connection.setInstanceFollowRedirects(false); redir = false; int stat = connection.getResponseCode(); if (stat >= 300 && stat <= 307 && stat != 306 && stat != HttpURLConnection.HTTP_NOT_MODIFIED) { URL base = connection.getURL(); redirectionUrl = connection.getHeaderField("Location"); URL target = null; if (redirectionUrl != null) { target = new URL(base, redirectionUrl); } connection.disconnect(); // Redirection should be allowed only for HTTP and HTTPS // and should be limited to 5 redirections at most. if (target == null || !(target.getProtocol().equals("http") || target.getProtocol().equals("https")) || redirects >= 5) { throw new SecurityException("illegal URL redirect"); } redir = true; connection = HttpTransportManager.openConnection(target, getBasicAuthUsername(), getBasicAuthPassword()); connection.setConnectTimeout(httpTimeout); connection.setReadTimeout(httpTimeout); redirects++; } } while (redir); return connection; }
From source file:net.sf.ehcache.constructs.web.filter.GzipFilterTest.java
/** * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED. * In this case there should not be a body but there is. Orion seems to kill the body * after is has left the Servlet filter chain. To avoid wget going into an inifinite * retry loop, and presumably some other web clients, the content length should be 0 * and the body 0./*ww w . ja v a 2s. c om*/ * <p/> * Manual test: wget -d --server-response --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp */ public void testNotModifiedJSPGzipFilter() throws Exception { String url = "http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp"; HttpClient httpClient = new HttpClient(); HttpMethod httpMethod = new GetMethod(url); httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT"); httpMethod.addRequestHeader("Accept-Encoding", "gzip"); int responseCode = httpClient.executeMethod(httpMethod); assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode); byte[] responseBody = httpMethod.getResponseBody(); assertEquals(null, responseBody); assertNull(httpMethod.getResponseHeader("Content-Encoding")); assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue()); checkNullOrZeroContentLength(httpMethod); }
From source file:org.apache.roller.weblogger.util.Blacklist.java
/** Download the MT blacklist from the web to our uploads directory. */ private boolean downloadBlacklist() { boolean blacklist_updated = false; try {// www. j a va 2 s. co m mLogger.debug("Attempting to download MT blacklist"); URL url = new URL(blacklistURL); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // after spending way too much time debugging i've discovered // that the blacklist server is selective based on the User-Agent // header. without this header set i always get a 403 response :( connection.setRequestProperty("User-Agent", "Mozilla/5.0"); if (this.lastModified != null) { connection.setRequestProperty("If-Modified-Since", DateUtil.formatRfc822(this.lastModified)); } int responseCode = connection.getResponseCode(); mLogger.debug("HttpConnection response = " + responseCode); // did the connection return NotModified? If so, no need to parse if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) { mLogger.debug("MT blacklist site says we are current"); return false; } // did the connection return a LastModified header? long lastModifiedLong = connection.getHeaderFieldDate("Last-Modified", -1); // if the file is newer than our current then we need do update it if (responseCode == HttpURLConnection.HTTP_OK && (this.lastModified == null || this.lastModified.getTime() < lastModifiedLong)) { mLogger.debug("my last modified = " + this.lastModified.getTime()); mLogger.debug("MT last modified = " + lastModifiedLong); // save the new blacklist InputStream instream = connection.getInputStream(); String uploadDir = WebloggerConfig.getProperty("uploads.dir"); String path = uploadDir + File.separator + blacklistFile; FileOutputStream outstream = new FileOutputStream(path); mLogger.debug("writing updated MT blacklist to " + path); // read from url and write to file byte[] buf = new byte[4096]; int length = 0; while ((length = instream.read(buf)) > 0) outstream.write(buf, 0, length); outstream.close(); instream.close(); blacklist_updated = true; mLogger.debug("MT blacklist download completed."); } else { mLogger.debug("blacklist *NOT* saved, assuming we are current"); } } catch (Exception e) { mLogger.error("error downloading blacklist", e); } return blacklist_updated; }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.Fetcher.java
private String fetchIfModifiedSince(final String url, final String data, final String method, final long lastFetchTime) throws IOException { final OutputStream out = null; try {/*w w w. ja v a 2 s . co m*/ final HttpURLConnection connection = getConnection(url, data, method, lastFetchTime); if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { return null; } if (connection.getResponseCode() > 399) { LOGGER.warn("Failed Http Request to " + url + " Status " + connection.getResponseCode()); return null; } final StringBuilder sb = new StringBuilder(); try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String input; while ((input = in.readLine()) != null) { sb.append(input); } } return sb.toString(); } finally { IOUtils.closeQuietly(out); } }
From source file:org.wso2.carbon.registry.app.ResourceServlet.java
/** * Logic that will be executed for a get request. * * @param request the HTTP Servlet request. * @param response the HTTP Servlet response. * * @throws IOException if an error occurred. *///from www . j a v a2 s. c om protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { try { String uri = request.getRequestURI(); int idx = uri.indexOf("resource"); String path = uri.substring(idx + 8); if (path == null) { String msg = "Could not get the resource content. Path is not specified."; log.error(msg); response.setStatus(HttpURLConnection.HTTP_BAD_REQUEST); return; } Resource resource; try { UserRegistry registry = Utils.getRegistry(request); try { path = new URI(path).normalize().toString(); } catch (URISyntaxException e) { log.error("Unable to normalize requested resource path: " + path, e); } String decodedPath = URLDecoder.decode(path, RegistryConstants.DEFAULT_CHARSET_ENCODING); CurrentSession.setUserRealm(registry.getUserRealm()); CurrentSession.setUser(registry.getUserName()); try { if (!AuthorizationUtils.authorize( RegistryUtils.getAbsolutePath(registry.getRegistryContext(), decodedPath), ActionConstants.GET)) { response.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\""); return; } resource = registry.get(decodedPath); } finally { CurrentSession.removeUserRealm(); CurrentSession.removeUser(); } } catch (AuthorizationFailedException e) { log.error(e.getMessage()); response.setStatus(HttpURLConnection.HTTP_UNAUTHORIZED); response.setHeader("WWW-Authenticate", "Basic realm=\"WSO2-Registry\""); return; } catch (RegistryException e) { String msg = "Error retrieving the resource " + path + ". " + e.getMessage(); log.error(msg, e); throw e; } if (resource instanceof Collection) { String msg = "Could not get the resource content. Path " + path + " refers to a collection."; log.error(msg); response.setStatus(HttpURLConnection.HTTP_NOT_IMPLEMENTED); return; } // date based conditional get long ifModifiedSinceValue = request.getDateHeader("If-Modified-Since"); long lastModifiedValue = resource.getLastModified().getTime(); if (ifModifiedSinceValue > 0) { // convert the time values from milliseconds to seconds ifModifiedSinceValue /= 1000; lastModifiedValue /= 1000; /* condition to check we have latest updates in terms of dates */ if (ifModifiedSinceValue >= lastModifiedValue) { /* no need to response with data */ response.setStatus(HttpURLConnection.HTTP_NOT_MODIFIED); return; } } response.setDateHeader("Last-Modified", lastModifiedValue); // eTag based conditional get String ifNonMatchValue = request.getHeader("if-none-match"); String currentETag = Utils.calculateEntityTag(resource); if (ifNonMatchValue != null) { if (ifNonMatchValue.equals(currentETag)) { /* the version is not modified */ response.setStatus(HttpURLConnection.HTTP_NOT_MODIFIED); return; } } response.setHeader("ETag", currentETag); if (resource.getMediaType() != null && resource.getMediaType().length() > 0) { response.setContentType(resource.getMediaType()); } else { response.setHeader("Content-Disposition", "attachment; filename=" + RegistryUtils.getResourceName(path)); response.setContentType("application/download"); } InputStream contentStream = null; if (resource.getContent() != null) { contentStream = resource.getContentStream(); } if (contentStream != null) { try { ServletOutputStream servletOutputStream = response.getOutputStream(); byte[] contentChunk = new byte[RegistryConstants.DEFAULT_BUFFER_SIZE]; int byteCount; while ((byteCount = contentStream.read(contentChunk)) != -1) { servletOutputStream.write(contentChunk, 0, byteCount); } response.flushBuffer(); servletOutputStream.flush(); } finally { contentStream.close(); } } else { Object content = resource.getContent(); if (content != null) { if (content instanceof byte[]) { ServletOutputStream servletOutputStream = response.getOutputStream(); servletOutputStream.write((byte[]) content); response.flushBuffer(); servletOutputStream.flush(); } else { PrintWriter writer = response.getWriter(); writer.write(content.toString()); writer.flush(); } } } resource.discard(); } catch (RegistryException e) { String msg = "Failed to get resource content. " + e.getMessage(); log.error(msg, e); response.setStatus(HttpURLConnection.HTTP_INTERNAL_ERROR); } }
From source file:de.schildbach.wallet.data.DynamicFeeLoader.java
private static void fetchDynamicFees(final HttpUrl url, final File tempFile, final File targetFile, final String userAgent) { final Stopwatch watch = Stopwatch.createStarted(); final Request.Builder request = new Request.Builder(); request.url(url);//from w ww . j a v a 2 s.co m request.header("User-Agent", userAgent); if (targetFile.exists()) request.header("If-Modified-Since", HttpDate.format(new Date(targetFile.lastModified()))); final OkHttpClient httpClient = Constants.HTTP_CLIENT.clone(); httpClient.setConnectTimeout(5, TimeUnit.SECONDS); httpClient.setWriteTimeout(5, TimeUnit.SECONDS); httpClient.setReadTimeout(5, TimeUnit.SECONDS); final Call call = httpClient.newCall(request.build()); try { final Response response = call.execute(); final int status = response.code(); if (status == HttpURLConnection.HTTP_NOT_MODIFIED) { log.info("Dynamic fees not modified at {}, took {}", url, watch); } else if (status == HttpURLConnection.HTTP_OK) { final ResponseBody body = response.body(); final FileOutputStream os = new FileOutputStream(tempFile); Io.copy(body.byteStream(), os); os.close(); final Date lastModified = response.headers().getDate("Last-Modified"); if (lastModified != null) tempFile.setLastModified(lastModified.getTime()); body.close(); if (!tempFile.renameTo(targetFile)) throw new IllegalStateException("Cannot rename " + tempFile + " to " + targetFile); watch.stop(); log.info("Dynamic fees fetched from {}, took {}", url, watch); } else { log.warn("HTTP status {} when fetching dynamic fees from {}", response.code(), url); } } catch (final Exception x) { log.warn("Problem when fetching dynamic fees rates from " + url, x); } }
From source file:org.omegat.core.team2.impl.HTTPRemoteRepository.java
/** * Retrieve remote URL with non-modified checking by ETag. If server doesn't support ETag, file will be * always retrieved./*ww w. jav a 2s .c o m*/ */ protected void retrieve(Properties etags, String file, String url, File out) throws Exception { String etag = etags.getProperty(file); Log.logDebug(LOGGER, "Retrieve " + url + " into " + out.getAbsolutePath() + " with ETag=" + etag); out.getParentFile().mkdirs(); HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); try { if (etag != null) { // use ETag if we know it conn.setRequestProperty("If-None-Match", etag); } switch (conn.getResponseCode()) { case HttpURLConnection.HTTP_OK: etag = conn.getHeaderField("ETag"); Log.logDebug(LOGGER, "Retrieve " + url + ": 200 with ETag=" + etag); break; case HttpURLConnection.HTTP_NOT_MODIFIED: // not modified - just return Log.logDebug(LOGGER, "Retrieve " + url + ": not modified"); return; default: throw new RuntimeException("HTTP response code: " + conn.getResponseCode()); } // load into .tmp file File temp = new File(out.getAbsolutePath() + ".tmp"); InputStream in = conn.getInputStream(); try { FileUtils.copyInputStreamToFile(in, temp); } finally { in.close(); } // rename into file if (out.exists()) { if (!out.delete()) { throw new IOException(); } } if (!temp.renameTo(out)) { throw new IOException(); } etags.setProperty(file, etag); } finally { conn.disconnect(); } Log.logDebug(LOGGER, "Retrieve " + url + " finished"); }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.Fetcher.java
public int getIfModifiedSince(final String url, final long lastFetchTime, final StringBuilder stringBuilder) throws IOException { final OutputStream out = null; try {/*from w ww .j a v a 2 s . c o m*/ final HttpURLConnection connection = getConnection(url, null, "GET", lastFetchTime); final int status = connection.getResponseCode(); if (status == HttpURLConnection.HTTP_NOT_MODIFIED) { return status; } if (connection.getResponseCode() > 399) { LOGGER.warn("Failed Http Request to " + url + " Status " + connection.getResponseCode()); return status; } try (final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) { String input; while ((input = in.readLine()) != null) { stringBuilder.append(input); } } return status; } finally { IOUtils.closeQuietly(out); } }
From source file:com.seajas.search.contender.http.HttpClientFeedFetcher.java
/** * Handle the response, making sure to take the Not-Modified header information into account. * /*from ww w . j a va2s. c om*/ * @param syndFeedInfo * @param url * @param response * @return SyndFeed * @throws IOException * @throws FetcherException * @throws FeedException */ private SyndFeed handleResponse(final SyndFeedInfo syndFeedInfo, final String url, final HttpResponse response) throws IOException, FetcherException, FeedException { if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_NOT_MODIFIED && syndFeedInfo != null) { fireEvent(FetcherEvent.EVENT_TYPE_FEED_UNCHANGED, url); return syndFeedInfo.getSyndFeed(); } SyndFeed feed = buildFeedFromResponse(url, response); fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, url, feed); return feed; }
From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java
public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation, CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion, final String localHostName) throws IOException, InterruptedException, HubIntegrationException, IllegalArgumentException, EncryptionException { boolean cliMismatch = true; try {// w w w. j a va 2s . c o m final File hubVersionFile = cliLocation.createHubVersionFile(); if (hubVersionFile.exists()) { final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile)); if (hubVersion.equals(storedHubVersion)) { cliMismatch = false; } else { hubVersionFile.delete(); hubVersionFile.createNewFile(); } } final File cliInstallDirectory = cliLocation.getCLIInstallDir(); if (!cliInstallDirectory.exists()) { cliMismatch = true; } if (cliMismatch) { logger.debug("Attempting to download the Hub CLI."); final FileWriter writer = new FileWriter(hubVersionFile); writer.write(hubVersion); writer.close(); hubVersionFile.setLastModified(0L); } final long cliTimestamp = hubVersionFile.lastModified(); URLConnection connection = null; try { Proxy proxy = null; if (hubProxyInfo != null) { String proxyHost = hubProxyInfo.getHost(); int proxyPort = hubProxyInfo.getPort(); String proxyUsername = hubProxyInfo.getUsername(); String proxyPassword = hubProxyInfo.getDecryptedPassword(); if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } if (proxy != null) { if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) { AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword); } else { AuthenticatorUtil.resetAuthenticator(); } } } if (proxy != null) { connection = archive.openConnection(proxy); } else { connection = archive.openConnection(); } connection.setIfModifiedSince(cliTimestamp); connection.connect(); } catch (final IOException ioe) { logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": " + ioe.toString()); return; } if (connection instanceof HttpURLConnection && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { // CLI has not been modified return; } final long sourceTimestamp = connection.getLastModified(); if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) { if (!cliMismatch && sourceTimestamp == cliTimestamp) { logger.debug("The current Hub CLI is up to date."); return; } for (final File file : cliInstallDirectory.listFiles()) { FileUtils.deleteDirectory(file); } } else { cliInstallDirectory.mkdir(); } logger.debug("Updating the Hub CLI."); hubVersionFile.setLastModified(sourceTimestamp); logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on " + localHostName); final CountingInputStream cis = new CountingInputStream(connection.getInputStream()); try { unzip(cliInstallDirectory, cis, logger); updateJreSecurity(logger, cliLocation, ciEnvironmentVariables); } catch (final IOException e) { throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), connection.getContentLength()), e); } } catch (final IOException e) { throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e); } }