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:com.rometools.fetcher.impl.HttpClientFeedFetcher.java
private SyndFeed getFeed(final SyndFeedInfo syndFeedInfo, final String urlStr, final HttpMethod method, final int statusCode) throws IOException, HttpException, FetcherException, FeedException { if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED && syndFeedInfo != null) { fireEvent(FetcherEvent.EVENT_TYPE_FEED_UNCHANGED, urlStr); return syndFeedInfo.getSyndFeed(); }/*from w ww. j a va2 s .c om*/ final SyndFeed feed = retrieveFeed(urlStr, method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, urlStr, feed); return feed; }
From source file:de.nava.informa.utils.FeedManagerEntry.java
/** * Updates the channel associated with this feed use conditional get stuff. * http://fishbowl.pastiche.org/2002/10/21/http_conditional_get_for_rss_hackers * * @throws FeedManagerException/*from www. j av a2s. c om*/ */ private synchronized void updateChannel() throws FeedManagerException { try { String feedUrl = this.feed.getLocation().toString(); URL aURL = new URL(feedUrl); URLConnection conn = aURL.openConnection(); if (conn instanceof HttpURLConnection) { HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setInstanceFollowRedirects(true); // Hack for User-Agent : problem for // http://www.diveintomark.org/xml/rss.xml HttpHeaderUtils.setUserAgent(httpConn, "Informa Java API"); HttpHeaderUtils.setETagValue(httpConn, this.httpHeaders.getETag()); HttpHeaderUtils.setIfModifiedSince(httpConn, this.httpHeaders.getIfModifiedSince()); httpConn.connect(); if (httpConn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { logger.info("cond. GET for feed at url " + feedUrl + ": no change"); this.feed.setLastUpdated(new Date()); // TODO : add a property in FeedIF interface for lastGet ? this.lastUpdate = System.currentTimeMillis(); return; } logger.info("cond. GET for feed at url " + feedUrl + ": changed"); logger.debug( "feed at url " + feedUrl + " new values : ETag" + HttpHeaderUtils.getETagValue(httpConn) + " if-modified :" + HttpHeaderUtils.getLastModified(httpConn)); this.httpHeaders.setETag(HttpHeaderUtils.getETagValue(httpConn)); this.httpHeaders.setIfModifiedSince(HttpHeaderUtils.getLastModified(httpConn)); } ChannelIF channel; if (conn == null) { channel = FeedParser.parse(getChannelBuilder(), feedUrl); } else { channel = FeedParser.parse(getChannelBuilder(), conn.getInputStream()); } this.feed.setChannel(channel); this.feed.setLastUpdated(new Date()); this.lastUpdate = System.currentTimeMillis(); logger.info("feed updated " + feedUrl); } catch (IOException | ParseException e) { throw new FeedManagerException(e); } }
From source file:org.lockss.config.HTTPConfigFile.java
private InputStream getUrlInputStream0(String url) throws IOException, MalformedURLException { InputStream in = null;//from ww w. ja v a 2 s. c om LockssUrlConnection conn = openUrlConnection(url); Configuration conf = ConfigManager.getPlatformConfig(); String proxySpec = conf.get(ConfigManager.PARAM_PROPS_PROXY); String proxyHost = null; int proxyPort = 0; try { HostPortParser hpp = new HostPortParser(proxySpec); proxyHost = hpp.getHost(); proxyPort = hpp.getPort(); } catch (HostPortParser.InvalidSpec e) { log.warning("Illegal props proxy: " + proxySpec, e); } if (proxyHost != null) { log.debug2("Setting request proxy to: " + proxyHost + ":" + proxyPort); conn.setProxy(proxyHost, proxyPort); } if (m_config != null && m_lastModified != null) { log.debug2("Setting request if-modified-since to: " + m_lastModified); conn.setIfModifiedSince(m_lastModified); } conn.setRequestProperty("Accept-Encoding", "gzip"); if (m_props != null) { Object x = m_props.get(Constants.X_LOCKSS_INFO); if (x instanceof String) { conn.setRequestProperty(Constants.X_LOCKSS_INFO, (String) x); } } conn.execute(); if (checkAuth && !conn.isAuthenticatedServer()) { IOUtil.safeRelease(conn); throw new IOException("Config server not authenticated"); } int resp = conn.getResponseCode(); String respMsg = conn.getResponseMessage(); log.debug2(url + " request got response: " + resp + ": " + respMsg); switch (resp) { case HttpURLConnection.HTTP_OK: m_loadError = null; m_httpLastModifiedString = conn.getResponseHeaderValue("last-modified"); log.debug2("New file, or file changed. Loading file from " + "remote connection:" + url); in = conn.getUncompressedResponseInputStream(); break; case HttpURLConnection.HTTP_NOT_MODIFIED: m_loadError = null; log.debug2("HTTP content not changed, not reloading."); IOUtil.safeRelease(conn); break; case HttpURLConnection.HTTP_NOT_FOUND: m_loadError = resp + ": " + respMsg; IOUtil.safeRelease(conn); throw new FileNotFoundException(m_loadError); case HttpURLConnection.HTTP_FORBIDDEN: m_loadError = findErrorMessage(resp, conn); IOUtil.safeRelease(conn); throw new IOException(m_loadError); default: m_loadError = resp + ": " + respMsg; IOUtil.safeRelease(conn); throw new IOException(m_loadError); } return in; }
From source file:com.twotoasters.android.hoottestapplication.test.HootTest.java
public void testGetAcceptNotModified() { final CountDownLatch latch = new CountDownLatch(1); final List<Integer> successfulResults = new ArrayList<Integer>(); successfulResults.add(HttpURLConnection.HTTP_NOT_MODIFIED); final HootRequest request = mHootRestClient.createRequest().get().setResource("error/304") .setSuccessfulResponseCodes(successfulResults).bindListener(new TestHootListener(latch, false)); assertNotNull(request);/*from ww w . java2 s . c om*/ executeTest(request, latch); assertTrue(request.getResult() != null && request.getResult().isSuccess() && request.getResult().getResponseCode() == 304); }
From source file:com.exzogeni.dk.http.HttpTask.java
@SuppressWarnings("checkstyle:illegalcatch") private V onSuccessInternal(HttpURLConnection cn) throws Exception { try {/*from w ww .j a va 2 s . c om*/ final URI uri = getEncodedUriInternal(); final Map<String, List<String>> headers = getHeaderFields(cn); final CookieManager cookieManager = mHttpManager.getCookieManager(); final CacheManager cacheManager = mHttpManager.getCacheManager(); cookieManager.put(uri, headers); final int responseCode = cn.getResponseCode(); InputStream content = getInputStream(cn); if (responseCode == HttpURLConnection.HTTP_OK && mCachePolicy.shouldCache(uri)) { content = cacheManager.put(uri, headers, content); } else if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED && mCachePolicy.shouldCache(uri)) { content = cacheManager.update(uri, headers); } return onSuccessInternal(responseCode, headers, content); } catch (Exception e) { throw onException(new HttpException(getEncodedUrlInternal(), e)); } }
From source file:org.apache.myfaces.renderkit.html.util.MyFacesResourceLoader.java
/** * Given a URI of form "{partial.class.name}/{resourceName}", locate the * specified file within the current classpath and write it to the * response object.//from w w w . j av a2 s . co m * <p> * The partial class name has "org.apache.myfaces.custom." prepended * to it to form the fully qualified classname. This class object is * loaded, and Class.getResourceAsStream is called on it, passing * a uri of "resource/" + {resourceName}. * <p> * The data written to the response stream includes http headers * which define the mime content-type; this is deduced from the * filename suffix of the resource. * <p> * @see org.apache.myfaces.renderkit.html.util.ResourceLoader#serveResource(javax.servlet.ServletContext, * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.String) */ public void serveResource(ServletContext context, HttpServletRequest request, HttpServletResponse response, String resourceUri) throws IOException { String[] uriParts = resourceUri.split("/", 2); String component = uriParts[0]; if (component == null || component.trim().length() == 0) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid request"); log.error("Could not find parameter for component to load a resource."); return; } Class componentClass; String className = ORG_APACHE_MYFACES_CUSTOM + "." + component; try { componentClass = loadComponentClass(className); } catch (ClassNotFoundException e) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); log.error("Could not find the class for component " + className + " to load a resource."); return; } String resource = uriParts[1]; if (resource == null || resource.trim().length() == 0) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No resource defined"); log.error("No resource defined component class " + className); return; } InputStream is = null; try { ResourceProvider resourceProvider; if (ResourceProvider.class.isAssignableFrom(componentClass)) { try { resourceProvider = (ResourceProvider) componentClass.newInstance(); } catch (InstantiationException e) { response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Unable to instantiate resource provider for resource " + resource + " for component " + component); log.error("Unable to instantiate resource provider for resource " + resource + " for component " + component, e); return; } catch (IllegalAccessException e) { response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "Unable to instantiate resource provider for resource " + resource + " for component " + component); log.error("Unable to instantiate resource provider for resource " + resource + " for component " + component, e); return; } } else { resourceProvider = new DefaultResourceProvider(componentClass); } if (!resourceProvider.exists(context, resource)) { response.sendError(HttpServletResponse.SC_NOT_FOUND, "Unable to find resource " + resource + " for component " + component + ". Check that this file is available " + "in the classpath in sub-directory " + "/resource of the package-directory."); log.error("Unable to find resource " + resource + " for component " + component + ". Check that this file is available " + "in the classpath in sub-directory " + "/resource of the package-directory."); } else { // URLConnection con = url.openConnection(); long lastModified = resourceProvider.getLastModified(context, resource); if (lastModified < 1) { // fallback lastModified = getLastModified(); } long browserDate = request.getDateHeader("If-Modified-Since"); if (browserDate > -1) { // normalize to seconds - this should work with any os lastModified = (lastModified / 1000) * 1000; browserDate = (browserDate / 1000) * 1000; if (lastModified == browserDate) { // the browser already has the correct version response.setStatus(HttpURLConnection.HTTP_NOT_MODIFIED); return; } } int contentLength = resourceProvider.getContentLength(context, resource); String contentEncoding = resourceProvider.getEncoding(context, resource); is = resourceProvider.getInputStream(context, resource); defineContentHeaders(request, response, resource, contentLength, contentEncoding); defineCaching(request, response, resource, lastModified); writeResource(request, response, is); } } finally { // nothing to do here.. } }
From source file:org.wso2.carbon.registry.app.RemoteRegistry.java
public Resource get(String path) throws RegistryException { AbderaClient abderaClient = new AbderaClient(abdera); ClientResponse clientResponse;/* w w w. j a va 2 s . c o m*/ String encodedPath; // If the request is to fetch all comments for a given path, then encode ":" as well to // avoid confusion with versioned paths. if (path.endsWith(RegistryConstants.URL_SEPARATOR + APPConstants.PARAMETER_COMMENTS)) { encodedPath = encodeURL(path); if (encodedPath.contains(RegistryConstants.VERSION_SEPARATOR)) { int index = encodedPath.lastIndexOf(RegistryConstants.VERSION_SEPARATOR); encodedPath = encodedPath.substring(0, index).replace(":", "%3A") + encodedPath.substring(index); } else { encodedPath = encodedPath.replace(":", "%3A"); } } else { encodedPath = encodeURL(path); } if (!cache.isResourceCached(path)) { clientResponse = abderaClient.get(baseURI + "/atom" + encodedPath, getAuthorization()); } else { clientResponse = abderaClient.get(baseURI + "/atom" + encodedPath, getAuthorizationForCaching(path)); } if (clientResponse.getType() == Response.ResponseType.CLIENT_ERROR || clientResponse.getType() == Response.ResponseType.SERVER_ERROR) { if (clientResponse.getStatus() == HttpURLConnection.HTTP_NOT_FOUND) { abderaClient.teardown(); throw new ResourceNotFoundException(path); } abderaClient.teardown(); throw new RegistryException(clientResponse.getStatusText()); } if (clientResponse.getStatus() == HttpURLConnection.HTTP_NOT_MODIFIED) { abderaClient.teardown(); /*do caching here */ log.debug("Cached resource returned since no modification has been done on the resource"); return cache.getCachedResource(path); } String eTag = clientResponse.getHeader("ETag"); Element introspection = clientResponse.getDocument().getRoot(); ResourceImpl resource; if (introspection instanceof Feed) { // This is a collection Feed feed = (Feed) introspection; String state = feed.getSimpleExtension(new QName(APPConstants.NAMESPACE, APPConstants.NAMESPACE_STATE)); if (state != null && state.equals("Deleted")) { abderaClient.teardown(); throw new ResourceNotFoundException(path); } resource = createResourceFromFeed(feed); } else { Entry entry = (Entry) introspection; resource = createResourceFromEntry(entry); } /* if the resource is not Get before add it to cache before adding it check the max cache or if the resource is modified then new resource is replacing the current resource in the cache * size configured in registry.xml */ if (!cache.cacheResource(path, resource, eTag, RegistryConstants.MAX_REG_CLIENT_CACHE_SIZE)) { log.debug("Max Cache size exceeded the configured Cache size"); } abderaClient.teardown(); // resource.setPath(path); return resource; }
From source file:osmcd.mapsources.loader.MapPackManager.java
public String downloadMD5SumList() throws IOException, UpdateFailedException { String md5eTag = Settings.getInstance().mapSourcesUpdate.etag; log.debug("Last md5 eTag: " + md5eTag); String updateUrl = System.getProperty("osmcd.updateurl"); if (updateUrl == null) throw new RuntimeException("Update url not present"); byte[] data = null; // Proxy p = new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("localhost", 8888)); HttpURLConnection conn = (HttpURLConnection) new URL(updateUrl).openConnection(); conn.setInstanceFollowRedirects(false); if (md5eTag != null) conn.addRequestProperty("If-None-Match", md5eTag); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) { log.debug("No newer md5 file available"); return null; }/*from w ww. j ava2s . c o m*/ if (responseCode != HttpURLConnection.HTTP_OK) throw new UpdateFailedException( "Invalid HTTP response: " + responseCode + " for update url " + conn.getURL()); // Case HTTP_OK InputStream in = conn.getInputStream(); data = Utilities.getInputBytes(in); in.close(); Settings.getInstance().mapSourcesUpdate.etag = conn.getHeaderField("ETag"); log.debug("New md5 file retrieved"); String md5sumList = new String(data); return md5sumList; }
From source file:mobac.mapsources.loader.MapPackManager.java
public String downloadMD5SumList() throws IOException, UpdateFailedException { String md5eTag = Settings.getInstance().mapSourcesUpdate.etag; log.debug("Last md5 eTag: " + md5eTag); String updateUrl = System.getProperty("mobac.updateurl"); if (updateUrl == null) throw new RuntimeException("Update url not present"); byte[] data = null; // Proxy p = new Proxy(Type.HTTP, InetSocketAddress.createUnresolved("localhost", 8888)); HttpURLConnection conn = (HttpURLConnection) new URL(updateUrl).openConnection(); conn.setInstanceFollowRedirects(false); if (md5eTag != null) conn.addRequestProperty("If-None-Match", md5eTag); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) { log.debug("No newer md5 file available"); return null; }//from ww w . j a v a 2s .c o m if (responseCode != HttpURLConnection.HTTP_OK) throw new UpdateFailedException( "Invalid HTTP response: " + responseCode + " for update url " + conn.getURL()); // Case HTTP_OK InputStream in = conn.getInputStream(); data = Utilities.getInputBytes(in); in.close(); Settings.getInstance().mapSourcesUpdate.etag = conn.getHeaderField("ETag"); log.debug("New md5 file retrieved"); String md5sumList = new String(data); return md5sumList; }