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:gr.scify.newsum.Utils.java
public static boolean urlChanged(String url) { try {// www . j av a 2 s . c om HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED); } catch (Exception e) { e.printStackTrace(); return false; } }
From source file:com.mbeddr.pluginmanager.com.intellij.ide.plugins.RepositoryHelper.java
@NotNull public static List<IdeaPluginDescriptor> loadPlugins(@Nullable String repositoryUrl, @Nullable BuildNumber buildnumber, @Nullable String channel, boolean forceHttps, @Nullable final ProgressIndicator indicator) throws IOException { String url;/*from w ww. ja va2s.co m*/ final File pluginListFile; final String host; try { URIBuilder uriBuilder; if (repositoryUrl == null) { uriBuilder = new URIBuilder(ApplicationInfoImpl.getShadowInstance().getPluginsListUrl()); pluginListFile = new File(PathManager.getPluginsPath(), channel == null ? PLUGIN_LIST_FILE : channel + "_" + PLUGIN_LIST_FILE); if (pluginListFile.length() > 0) { uriBuilder.addParameter("crc32", Files.hash(pluginListFile, Hashing.crc32()).toString()); } } else { uriBuilder = new URIBuilder(repositoryUrl); pluginListFile = null; } if (!URLUtil.FILE_PROTOCOL.equals(uriBuilder.getScheme())) { uriBuilder.addParameter("build", (buildnumber != null ? buildnumber.asString() : ApplicationInfoImpl.getShadowInstance().getApiVersion())); if (channel != null) uriBuilder.addParameter("channel", channel); } host = uriBuilder.getHost(); url = uriBuilder.build().toString(); } catch (URISyntaxException e) { throw new IOException(e); } if (indicator != null) { indicator.setText2(IdeBundle.message("progress.connecting.to.plugin.manager", host)); } RequestBuilder request = HttpRequests.request(url).forceHttps(forceHttps); return process(repositoryUrl, request.connect(new HttpRequests.RequestProcessor<List<IdeaPluginDescriptor>>() { @Override public List<IdeaPluginDescriptor> process(@NotNull HttpRequests.Request request) throws IOException { if (indicator != null) { indicator.checkCanceled(); } URLConnection connection = request.getConnection(); if (pluginListFile != null && pluginListFile.length() > 0 && connection instanceof HttpURLConnection && ((HttpURLConnection) connection) .getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { return loadPluginList(pluginListFile); } if (indicator != null) { indicator.checkCanceled(); indicator.setText2(IdeBundle.message("progress.downloading.list.of.plugins", host)); } if (pluginListFile != null) { synchronized (RepositoryHelper.class) { FileUtil.ensureExists(pluginListFile.getParentFile()); request.saveToFile(pluginListFile, indicator); return loadPluginList(pluginListFile); } } else { return parsePluginList(request.getReader()); } } })); }
From source file:org.wso2.developerstudio.eclipse.registry.base.remote.RemoteRegistry.java
public Resource get(String path) throws RegistryException { AbderaClient abderaClient = new AbderaClient(abdera); AbderaClient.registerTrustManager(new TrustEverythingTrustManager()); ClientResponse clientResponse;//from w w w.j av a2 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:com.xpn.xwiki.plugin.feed.XWikiFeedFetcher.java
private SyndFeed getFeed(SyndFeedInfo syndFeedInfo, String urlStr, HttpMethod method, int statusCode) throws IOException, FetcherException, FeedException { if (statusCode == HttpURLConnection.HTTP_NOT_MODIFIED && syndFeedInfo != null) { fireEvent(FetcherEvent.EVENT_TYPE_FEED_UNCHANGED, urlStr); return syndFeedInfo.getSyndFeed(); }/*from w w w .ja v a2 s . c o m*/ SyndFeed feed = retrieveFeed(urlStr, method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, urlStr, feed); return feed; }
From source file:fi.cosky.sdk.API.java
private <T extends BaseData> T sendRequest(Link l, Class<T> tClass, Object object) throws IOException { URL serverAddress;/*from w ww. ja v a 2s. co m*/ BufferedReader br; String result = ""; HttpURLConnection connection = null; String url = l.getUri().contains("://") ? l.getUri() : this.baseUrl + l.getUri(); try { String method = l.getMethod(); String type = l.getType(); serverAddress = new URL(url); connection = (HttpURLConnection) serverAddress.openConnection(); boolean doOutput = doOutput(method); connection.setDoOutput(doOutput); connection.setRequestMethod(method); connection.setInstanceFollowRedirects(false); if (method.equals("GET") && useMimeTypes) if (type == null || type.equals("")) { addMimeTypeAcceptToRequest(object, tClass, connection); } else { connection.addRequestProperty("Accept", helper.getSupportedType(type)); } if (!useMimeTypes) connection.setRequestProperty("Accept", "application/json"); if (doOutput && useMimeTypes) { //this handles the case if the link is self made and the type field has not been set. if (type == null || type.equals("")) { addMimeTypeContentTypeToRequest(l, tClass, connection); addMimeTypeAcceptToRequest(l, tClass, connection); } else { connection.addRequestProperty("Accept", helper.getSupportedType(type)); connection.addRequestProperty("Content-Type", helper.getSupportedType(type)); } } if (!useMimeTypes) connection.setRequestProperty("Content-Type", "application/json"); if (tokenData != null) { connection.addRequestProperty("Authorization", tokenData.getTokenType() + " " + tokenData.getAccessToken()); } addVersionNumberToHeader(object, url, connection); if (method.equals("POST") || method.equals("PUT")) { String json = object != null ? gson.toJson(object) : ""; //should handle the case when POST without object. connection.addRequestProperty("Content-Length", json.getBytes("UTF-8").length + ""); OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream()); osw.write(json); osw.flush(); osw.close(); } connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_CREATED || connection.getResponseCode() == HttpURLConnection.HTTP_SEE_OTHER) { ResponseData data = new ResponseData(); Link link = parseLocationLinkFromString(connection.getHeaderField("Location")); link.setType(type); data.setLocation(link); connection.disconnect(); return (T) data; } if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) { System.out.println( "Authentication expired " + connection.getResponseMessage() + " trying to reauthenticate"); if (retry && this.tokenData != null) { this.tokenData = null; retry = false; if (authenticate()) { System.out.println("Reauthentication success, will continue with " + l.getMethod() + " request on " + l.getRel()); return sendRequest(l, tClass, object); } } else throw new IOException( "Tried to reauthenticate but failed, please check the credentials and status of NFleet-API"); } if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { return (T) objectCache.getObject(url); } if (connection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT) { return (T) new ResponseData(); } if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST && connection.getResponseCode() < HttpURLConnection.HTTP_INTERNAL_ERROR) { System.out.println("ErrorCode: " + connection.getResponseCode() + " " + connection.getResponseMessage() + " " + url + ", verb: " + method); String errorString = readErrorStreamAndCloseConnection(connection); throw (NFleetRequestException) gson.fromJson(errorString, NFleetRequestException.class); } else if (connection.getResponseCode() >= HttpURLConnection.HTTP_INTERNAL_ERROR) { if (retry) { System.out.println("Request caused internal server error, waiting " + RETRY_WAIT_TIME + " ms and trying again."); return waitAndRetry(connection, l, tClass, object); } else { System.out.println("Requst caused internal server error, please contact dev@nfleet.fi"); String errorString = readErrorStreamAndCloseConnection(connection); throw new IOException(errorString); } } if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_GATEWAY) { if (retry) { System.out.println("Could not connect to NFleet-API, waiting " + RETRY_WAIT_TIME + " ms and trying again."); return waitAndRetry(connection, l, tClass, object); } else { System.out.println( "Could not connect to NFleet-API, please check service status from http://status.nfleet.fi and try again later."); String errorString = readErrorStreamAndCloseConnection(connection); throw new IOException(errorString); } } result = readDataFromConnection(connection); } catch (MalformedURLException e) { throw e; } catch (ProtocolException e) { throw e; } catch (UnsupportedEncodingException e) { throw e; } catch (IOException e) { throw e; } catch (SecurityException e) { throw e; } catch (IllegalArgumentException e) { throw e; } finally { assert connection != null; connection.disconnect(); } Object newEntity = gson.fromJson(result, tClass); objectCache.addUri(url, newEntity); return (T) newEntity; }
From source file:com.sun.syndication.fetcher.impl.HttpClientFeedFetcher.java
private SyndFeed getFeed(SyndFeedInfo syndFeedInfo, String urlStr, HttpMethod method, 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(); }// w w w. jav a 2s. co m SyndFeed feed = retrieveFeed(urlStr, method); fireEvent(FetcherEvent.EVENT_TYPE_FEED_RETRIEVED, urlStr, feed); return feed; }
From source file:com.adobe.phonegap.contentsync.Sync.java
private boolean download(final String source, final File file, final JSONObject headers, final ProgressEvent progress, final CallbackContext callbackContext, final boolean trustEveryone) { Log.d(LOG_TAG, "download " + source); if (!Patterns.WEB_URL.matcher(source).matches()) { sendErrorMessage("Invalid URL", INVALID_URL_ERROR, callbackContext); return false; }/*from w w w . j a v a2 s. c o m*/ final CordovaResourceApi resourceApi = webView.getResourceApi(); final Uri sourceUri = resourceApi.remapUri(Uri.parse(source)); int uriType = CordovaResourceApi.getUriType(sourceUri); final boolean useHttps = uriType == CordovaResourceApi.URI_TYPE_HTTPS; final boolean isLocalTransfer = !useHttps && uriType != CordovaResourceApi.URI_TYPE_HTTP; synchronized (progress) { if (progress.isAborted()) { return false; } } HttpURLConnection connection = null; HostnameVerifier oldHostnameVerifier = null; SSLSocketFactory oldSocketFactory = null; PluginResult result = null; TrackingInputStream inputStream = null; boolean cached = false; OutputStream outputStream = null; try { OpenForReadResult readResult = null; final Uri targetUri = resourceApi.remapUri(Uri.fromFile(file)); progress.setTargetFile(file); progress.setStatus(STATUS_DOWNLOADING); Log.d(LOG_TAG, "Download file: " + sourceUri); Log.d(LOG_TAG, "Target file: " + file); Log.d(LOG_TAG, "size = " + file.length()); if (isLocalTransfer) { readResult = resourceApi.openForRead(sourceUri); if (readResult.length != -1) { progress.setTotal(readResult.length); } inputStream = new SimpleTrackingInputStream(readResult.inputStream); } else { // connect to server // Open a HTTP connection to the URL based on protocol connection = resourceApi.createHttpConnection(sourceUri); if (useHttps && trustEveryone) { // Setup the HTTPS connection class to trust everyone HttpsURLConnection https = (HttpsURLConnection) connection; oldSocketFactory = trustAllHosts(https); // Save the current hostnameVerifier oldHostnameVerifier = https.getHostnameVerifier(); // Setup the connection not to verify hostnames https.setHostnameVerifier(DO_NOT_VERIFY); } connection.setRequestMethod("GET"); // TODO: Make OkHttp use this CookieManager by default. String cookie = getCookies(sourceUri.toString()); if (cookie != null) { connection.setRequestProperty("cookie", cookie); } // This must be explicitly set for gzip progress tracking to work. connection.setRequestProperty("Accept-Encoding", "gzip"); // Handle the other headers if (headers != null) { addHeadersToRequest(connection, headers); } connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { cached = true; connection.disconnect(); sendErrorMessage("Resource not modified: " + source, CONNECTION_ERROR, callbackContext); return false; } else { if (connection.getContentEncoding() == null || connection.getContentEncoding().equalsIgnoreCase("gzip")) { // Only trust content-length header if we understand // the encoding -- identity or gzip int connectionLength = connection.getContentLength(); if (connectionLength != -1) { if (connectionLength > getFreeSpace()) { cached = true; connection.disconnect(); sendErrorMessage("Not enough free space to download", CONNECTION_ERROR, callbackContext); return false; } else { progress.setTotal(connectionLength); } } } inputStream = getInputStream(connection); } } if (!cached) { try { synchronized (progress) { if (progress.isAborted()) { return false; } //progress.connection = connection; } // write bytes to file byte[] buffer = new byte[MAX_BUFFER_SIZE]; int bytesRead = 0; outputStream = resourceApi.openOutputStream(targetUri); while ((bytesRead = inputStream.read(buffer)) > 0) { synchronized (progress) { if (progress.isAborted()) { return false; } } Log.d(LOG_TAG, "bytes read = " + bytesRead); outputStream.write(buffer, 0, bytesRead); // Send a progress event. progress.setLoaded(inputStream.getTotalRawBytesRead()); updateProgress(callbackContext, progress); } } finally { synchronized (progress) { //progress.connection = null; } safeClose(inputStream); safeClose(outputStream); } } } catch (Throwable e) { sendErrorMessage(e.getLocalizedMessage(), CONNECTION_ERROR, callbackContext); } finally { if (connection != null) { // Revert back to the proper verifier and socket factories if (trustEveryone && useHttps) { HttpsURLConnection https = (HttpsURLConnection) connection; https.setHostnameVerifier(oldHostnameVerifier); https.setSSLSocketFactory(oldSocketFactory); } } } return true; }
From source file:com.comcast.cdn.traffic_control.traffic_router.core.loc.AbstractServiceUpdater.java
protected File downloadDatabase(final String url, final File existingDb) throws IOException { LOGGER.info("[" + getClass().getSimpleName() + "] Downloading database: " + url); final URL dbURL = new URL(url); final HttpURLConnection conn = (HttpURLConnection) dbURL.openConnection(); if (useModifiedTimestamp(existingDb)) { conn.setIfModifiedSince(existingDb.lastModified()); if (eTag != null) { conn.setRequestProperty("If-None-Match", eTag); }/*from www. j av a 2 s. co m*/ } InputStream in = conn.getInputStream(); eTag = conn.getHeaderField("ETag"); if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { LOGGER.info("[" + getClass().getSimpleName() + "] " + url + " not modified since our existing database's last update time of " + new Date(existingDb.lastModified())); return existingDb; } if (sourceCompressed) { in = new GZIPInputStream(in); } final File outputFile = File.createTempFile(tmpPrefix, tmpSuffix); final OutputStream out = new FileOutputStream(outputFile); IOUtils.copy(in, out); IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); return outputFile; }
From source file:com.wanikani.wklib.Connection.java
protected Response call(Meter meter, String resource, boolean isArray, String arg, CacheInfo cinfo) throws IOException { HttpURLConnection conn;//from www .j av a2 s . co m JSONTokener tok; InputStream is; URL url; url = new URL(makeURL(resource, arg)); conn = null; tok = null; try { conn = (HttpURLConnection) url.openConnection(); if (cinfo != null) { if (cinfo.etag != null) conn.setRequestProperty("If-None-Match", cinfo.etag); else if (cinfo.modified != null) conn.setIfModifiedSince(cinfo.modified.getTime()); } setTimeouts(conn); conn.connect(); if (cinfo != null && cinfo.hasData() && conn.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) throw new NotModifiedException(); measureHeaders(meter, conn, false); is = conn.getInputStream(); tok = new JSONTokener(readStream(meter, is)); } finally { if (conn != null) conn.disconnect(); } if (cinfo != null) { cinfo.modified = new Date(); if (conn.getDate() > 0) cinfo.modified = new Date(conn.getDate()); if (conn.getLastModified() > 0) cinfo.modified = new Date(conn.getLastModified()); cinfo.etag = conn.getHeaderField("ETag"); } try { return new Response(new JSONObject(tok), isArray); } catch (JSONException e) { throw new ParseException(); } }
From source file:at.spardat.xma.boot.transport.HTTPTransport.java
private Result getResourceImpl(IRtXMASessionClient session, URL url, long modifiedSince, String etag) throws CommunicationException { /* locals ---------------------------------- */ Result result = new Result(); int code = 0; HttpURLConnection conn;//from w w w .j a v a2 s . co m /* locals ---------------------------------- */ try { conn = (HttpURLConnection) url.openConnection(); if (conn instanceof HttpsURLConnection) { ((HttpsURLConnection) conn).setHostnameVerifier(hostnameVerifier); } sendCookies(session, url, conn); if (etag != null) { conn.setRequestProperty("If-None-Match", etag); //$NON-NLS-1$ } String strUrl = url.toExternalForm(); if (url.getQuery() == null && (strUrl.endsWith(".jar") || strUrl.endsWith(".xml"))) { conn.setRequestProperty(Statics.HTTP_CACHE_CONTROL, Statics.HTTP_MAX_AGE + "=0"); //$NON-NLS-1$ } if (modifiedSince > 0) { // see sun bugid: 4397096 // if HTTP_Util library is used, the original method may also be used. // conn.setIfModifiedSince(modifiedSince); conn.setRequestProperty(Statics.strIfModifiedSince, HTTPTransport.httpDate(modifiedSince)); } conn.setRequestProperty(Statics.HTTP_ACCEPT, "*/*"); //$NON-NLS-1$ conn.setRequestProperty(Statics.HTTP_USER_AGENT, Statics.HTTP_USER_AGENT_NAME); } catch (IOException exc) { log_.log(LogLevel.WARNING, "error loading '" + url.toString() + "' form server:", exc); //$NON-NLS-1$ throw new ConnectException("error loading '" + url.toString() + "' form server:", exc); } try { code = conn.getResponseCode(); if (code == HttpURLConnection.HTTP_NOT_MODIFIED) { result.contentLength_ = 0; result.lastModified_ = conn.getLastModified(); if (result.lastModified_ <= 0) { result.lastModified_ = modifiedSince; } result.expirationDate_ = conn.getExpiration(); result.etag_ = conn.getHeaderField(Statics.strEtag); if (result.etag_ == null) { result.etag_ = etag; } log_.log(LogLevel.FINE, "resource not modified: {0}", url.toExternalForm()); //$NON-NLS-1$ } else if (code == HttpURLConnection.HTTP_OK) { result.contentLength_ = conn.getContentLength(); result.lastModified_ = conn.getLastModified(); result.expirationDate_ = conn.getExpiration(); result.etag_ = conn.getHeaderField(Statics.strEtag); result.transformations_ = conn.getHeaderField(Statics.TRANSFORM_HEADER); result.setBuffer(this.readOutput(conn)); if (result.contentLength_ < 0) { result.contentLength_ = result.buffer_.length; } } else if (code == HttpURLConnection.HTTP_MOVED_TEMP || code == HttpURLConnection.HTTP_MOVED_PERM) { String location = conn.getHeaderField(Statics.HTTP_LOCATION); throw new RedirectException("redirect received from " + url.toString() + " to " + location, code, location); } else { if (code < 500) throw new ConnectException("error loading '" + url.toString() + "' from the server:", code); else throw new ServerException("error loading '" + url.toString() + "' from the server:", code); } readCookies(session, url, conn); } catch (RedirectException re) { throw re; } catch (CommunicationException ce) { if (code != 0) log_.log(LogLevel.WARNING, "http returncode: {0}", Integer.toString(code)); //$NON-NLS-1$ log_.log(LogLevel.WARNING, "error loading '" + url.toString() + "' from the server:", ce); //$NON-NLS-1$ throw ce; } catch (Exception ex) { if (code != 0) log_.log(LogLevel.WARNING, "http returncode: {0}", Integer.toString(code)); //$NON-NLS-1$ log_.log(LogLevel.WARNING, "error loading '" + url.toString() + "' from the server:", ex); //$NON-NLS-1$ if (code < 500) throw new ConnectException("error loading '" + url.toString() + "' from the server:", ex); else throw new ServerException("error loading '" + url.toString() + "' from the server:", ex); } return result; }