Example usage for java.net HttpURLConnection setIfModifiedSince

List of usage examples for java.net HttpURLConnection setIfModifiedSince

Introduction

In this page you can find the example usage for java.net HttpURLConnection setIfModifiedSince.

Prototype

public void setIfModifiedSince(long ifmodifiedsince) 

Source Link

Document

Sets the value of the ifModifiedSince field of this URLConnection to the specified value.

Usage

From source file:org.openflexo.toolbox.FileUtils.java

public static String createOrUpdateFileFromURL(URL url, File file, Map<String, String> headers) {
    long lastModified = 0;
    String fileContent = null;/* w  w w  .j  a  v a2 s .c om*/
    if (file.exists()) {
        lastModified = file.lastModified();
        try {
            fileContent = FileUtils.fileContents(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (url != null) {
        try {
            URLConnection c = url.openConnection();
            if (headers != null) {
                for (Map.Entry<String, String> h : headers.entrySet()) {
                    c.addRequestProperty(h.getKey(), h.getValue());
                }
            }
            if (c instanceof HttpURLConnection) {
                HttpURLConnection connection = (HttpURLConnection) c;
                connection.setIfModifiedSince(lastModified);
                connection.connect();
                if (connection.getResponseCode() == 200) {
                    fileContent = FileUtils.fileContents(connection.getInputStream(), "UTF-8");
                    FileUtils.saveToFile(file, fileContent);
                }
            } else {
                if (c.getDate() == 0 || c.getDate() > lastModified) {
                    fileContent = FileUtils.fileContents(c.getInputStream(), "UTF-8");
                    FileUtils.saveToFile(file, fileContent);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return fileContent;
}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected int getStatus(String url, long lastUpdate) throws IOException {
    HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
    try {/*from w  w  w .j  ava 2 s . co m*/
        if (lastUpdate > 0) {
            c.setIfModifiedSince(lastUpdate);
        }
        c.setUseCaches(false);
        c.connect();
        return c.getResponseCode();
    } finally {
        c.disconnect();
    }
}

From source file:net.gcolin.simplerepo.test.AbstractRepoTest.java

protected String getContent(String url, long lastUpdate) throws IOException {
    HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
    try {//  ww  w .  ja va2 s .  co m
        if (lastUpdate > 0) {
            c.setIfModifiedSince(lastUpdate);
        }
        c.setUseCaches(false);
        c.connect();
        return c.getResponseCode() == HttpURLConnection.HTTP_OK
                ? new String(IOUtils.toByteArray(c.getInputStream()), "utf-8")
                : null;
    } finally {
        c.disconnect();
    }
}

From source file:org.droidparts.net.http.RESTClient.java

public HTTPResponse get(String uri, long ifModifiedSince, String etag, boolean body) throws HTTPException {
    L.i("GET on '%s', If-Modified-Since: '%d', ETag: '%s', body: '%b'.", uri, ifModifiedSince, etag, body);
    HTTPResponse response;// ww w .  ja v a2 s.co  m
    if (useHttpURLConnection()) {
        HttpURLConnection conn = httpURLConnectionWorker.getConnection(uri, Method.GET);
        if (ifModifiedSince > 0) {
            conn.setIfModifiedSince(ifModifiedSince);
        }
        if (etag != null) {
            conn.addRequestProperty(Header.IF_NONE_MATCH, etag);
        }
        response = HttpURLConnectionWorker.getReponse(conn, body);
    } else {
        HttpGet req = new HttpGet(uri);
        if (ifModifiedSince > 0) {
            req.addHeader(Header.IF_MODIFIED_SINCE, new Date(ifModifiedSince).toGMTString());
        }
        if (etag != null) {
            req.addHeader(Header.IF_NONE_MATCH, etag);
        }
        response = httpClientWorker.getReponse(req, body);
    }
    L.d(response);
    return response;
}

From source file:edu.mit.mobile.android.net.DownloadLoader.java

@Override
public Uri loadInBackground() {
    boolean alreadyHasDownload = false;

    if (!outdir.exists() && !outdir.mkdirs()) {
        mException = new IOException("could not mkdirs: " + outdir.getAbsolutePath());
        return null;
    }/*from w  w  w  .ja  va 2  s . c  o  m*/
    final String fname = mUrl.substring(mUrl.lastIndexOf('/'));
    final File outfile = new File(outdir, fname);

    alreadyHasDownload = outfile.exists() && outfile.length() > MINIMUM_REASONABLE_VIDEO_SIZE;

    final long lastModified = outfile.exists() ? outfile.lastModified() : 0;

    try {
        final NetworkInfo netInfo = mCm.getActiveNetworkInfo();
        if (netInfo == null || !netInfo.isConnected()) {
            // no connection, but there's already a file. Hopefully it works!
            if (alreadyHasDownload) {
                return Uri.fromFile(outfile);
            } else {
                mException = new IOException(getContext().getString(R.string.err_no_data_connection));
                return null;
            }
        }

        HttpURLConnection hc = (HttpURLConnection) new URL(mUrl).openConnection();

        hc.setInstanceFollowRedirects(true);

        if (lastModified != 0) {
            hc.setIfModifiedSince(lastModified);
        }

        int resp = hc.getResponseCode();

        if (resp >= 300 && resp < 400) {

            final String redirectUrl = hc.getURL().toExternalForm();

            if (BuildConfig.DEBUG) {
                Log.d(TAG, "following redirect to " + redirectUrl);
            }
            hc = (HttpURLConnection) new URL(redirectUrl).openConnection();

            if (lastModified != 0) {
                hc.setIfModifiedSince(lastModified);
            }
            resp = hc.getResponseCode();
        }

        if (resp != HttpStatus.SC_OK && resp != HttpStatus.SC_NOT_MODIFIED) {
            Log.e(TAG, "got a non-200 response from server");
            mException = new NetworkProtocolException("Received " + resp + " response from server");
            return null;
        }
        if (resp == HttpStatus.SC_NOT_MODIFIED) {
            if (Constants.DEBUG) {
                Log.d(TAG, "got NOT MODIFIED");
            }
            // verify the integrity of the file
            if (alreadyHasDownload) {
                if (Constants.DEBUG) {
                    Log.d(TAG, fname + " has not been modified since it was downloaded last");
                }
                return Uri.fromFile(outfile);
            } else {
                // re-request without the if-modified header. This shouldn't happen.
                hc = (HttpURLConnection) new URL(mUrl).openConnection();
                final int responseCode = hc.getResponseCode();
                if (responseCode != HttpStatus.SC_OK) {
                    Log.e(TAG, "got a non-200 response from server");
                    mException = new NetworkProtocolException(
                            "Received " + responseCode + " response from server");
                    return null;
                }
            }
        }

        final int contentLength = hc.getContentLength();

        if (contentLength == 0) {
            Log.e(TAG, "got an empty response from server");
            mException = new IOException("Received an empty response from server.");
            return null;

        } else if (contentLength < MINIMUM_REASONABLE_VIDEO_SIZE) { // this is probably not a
                                                                    // video
            Log.e(TAG, "got a very small response from server of length " + hc.getContentLength());
            mException = new IOException("Received an unusually-small response from server.");
            return null;
        }
        boolean downloadSucceeded = false;
        try {

            final BufferedInputStream bis = new BufferedInputStream(hc.getInputStream());
            final FileOutputStream fos = new FileOutputStream(outfile);
            if (Constants.DEBUG) {
                Log.d(TAG, "downloading...");
            }
            StreamUtils.inputStreamToOutputStream(bis, fos);

            fos.close();

            // store the server's last modified date in the filesystem
            outfile.setLastModified(hc.getLastModified());

            downloadSucceeded = true;
            if (Constants.DEBUG) {
                Log.d(TAG, "done! Saved to " + outfile);
            }
            return Uri.fromFile(outfile);

        } finally {
            hc.disconnect();
            // cleanup if this is the first attempt to download
            if (!alreadyHasDownload && !downloadSucceeded) {
                outfile.delete();
            }
        }
    } catch (final IOException e) {
        Log.e(TAG, "error downloading file", e);
        mException = e;
        return null;
    }
}

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  w  w w  . j av a2  s.c  o  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;
    JSONTokener tok;//from  ww  w.j  a  va  2s . c  om
    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:de.alosdev.android.customerschoice.CustomersChoice.java

private void internalConfigureByNetwork(final Context context, String fileAddress) {
    new AsyncTask<String, Void, Void>() {
        @Override/*  www  .  j  a  va  2 s  . com*/
        protected Void doInBackground(String... args) {
            String value = args[0];
            try {
                final SharedPreferences preferences = getPreferences(context);
                final URL url = new URL(value);
                log.d(TAG, "read from: ", value);

                final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);

                // set etag header if existing
                final String fieldEtag = preferences.getString(getPreferencesKey(value, FIELD_ETAG), null);
                if (null != fieldEtag) {
                    conn.setRequestProperty("If-None-Match", fieldEtag);
                }

                // set modified since header if existing
                final long fieldLastModified = preferences
                        .getLong(getPreferencesKey(value, FIELD_LAST_MODIFIED), 0);
                if (fieldLastModified > 0) {
                    conn.setIfModifiedSince(fieldLastModified);
                }
                conn.connect();

                final int response = conn.getResponseCode();

                if (HttpStatus.SC_OK == response) {
                    log.d(TAG, "found file");
                    readFromInputStream(conn.getInputStream());

                    // writing caching information into preferences
                    final Editor editor = preferences.edit();
                    editor.putString(getPreferencesKey(value, FIELD_ETAG), conn.getHeaderField("ETag"));
                    editor.putLong(getPreferencesKey(value, FIELD_LAST_MODIFIED),
                            conn.getHeaderFieldDate("Last-Modified", 0));
                    editor.commit();
                } else if (HttpStatus.SC_NOT_MODIFIED == response) {
                    log.i(TAG, "no updates, file not modified: ", value);
                } else {
                    log.e(TAG, "cannot read from: ", value, " and get following response code:", response);
                }
            } catch (MalformedURLException e) {
                log.e(TAG, e, "the given URL is malformed: ", value);
            } catch (IOException e) {
                log.e(TAG, e, "Error during reading the file: ", value);
            }
            return null;
        }

    }.execute(fileAddress);
}

From source file:org.opencms.staticexport.CmsAfterPublishStaticExportHandler.java

/**
 * Exports a single (template) resource specified by its export data.<p>
 * //from www.j av  a  2  s .co m
 * @param data the export data
 * @param cookies cookies to keep the session
 * 
 * @return the status of the http request used to perform the export
 * 
 * @throws IOException if the http request fails
 */
protected int exportTemplateResource(CmsStaticExportData data, StringBuffer cookies) throws IOException {

    String vfsName = data.getVfsName();
    String rfsName = data.getRfsName();
    CmsStaticExportManager manager = OpenCms.getStaticExportManager();

    String exportUrlStr;
    if (rfsName.contains(manager.getRfsPrefix(vfsName))) {
        LOG.info("rfsName " + rfsName + " contains rfsPrefix " + manager.getRfsPrefix(vfsName));
        exportUrlStr = manager.getExportUrl() + rfsName;
    } else {
        exportUrlStr = manager.getExportUrl() + manager.getRfsPrefix(vfsName) + rfsName;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(Messages.get().getBundle().key(Messages.LOG_SENDING_REQUEST_2, rfsName, exportUrlStr));
    }
    // setup the connection and request the resource
    URL exportUrl = new URL(exportUrlStr);
    HttpURLConnection.setFollowRedirects(false);
    HttpURLConnection urlcon = (HttpURLConnection) exportUrl.openConnection();
    // set request type to GET
    urlcon.setRequestMethod(REQUEST_METHOD_GET);
    // add special export header
    urlcon.setRequestProperty(CmsRequestUtil.HEADER_OPENCMS_EXPORT, CmsStringUtil.TRUE);
    // add additional headers if available
    if (manager.getAcceptLanguageHeader() != null) {
        urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_LANGUAGE, manager.getAcceptLanguageHeader());
    } else {
        urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_LANGUAGE,
                manager.getDefaultAcceptLanguageHeader());
    }
    if (manager.getAcceptCharsetHeader() != null) {
        urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_CHARSET, manager.getAcceptCharsetHeader());
    } else {
        urlcon.setRequestProperty(CmsRequestUtil.HEADER_ACCEPT_CHARSET,
                manager.getDefaultAcceptCharsetHeader());
    }

    // get the last modified date and add it to the request
    String exportFileName = CmsFileUtil.normalizePath(manager.getExportPath(vfsName) + rfsName);
    File exportFile = new File(exportFileName);
    long dateLastModified = exportFile.lastModified();
    // system folder case
    if (vfsName.startsWith(CmsWorkplace.VFS_PATH_SYSTEM)) {
        // iterate over all rules
        Iterator<CmsStaticExportRfsRule> it = manager.getRfsRules().iterator();
        while (it.hasNext()) {
            CmsStaticExportRfsRule rule = it.next();
            if (rule.match(vfsName)) {
                exportFileName = CmsFileUtil.normalizePath(rule.getExportPath() + rfsName);
                exportFile = new File(exportFileName);
                if (dateLastModified > exportFile.lastModified()) {
                    dateLastModified = exportFile.lastModified();
                }
            }
        }
    }
    urlcon.setIfModifiedSince(dateLastModified);
    if (LOG.isDebugEnabled()) {
        LOG.debug(Messages.get().getBundle().key(Messages.LOG_IF_MODIFIED_SINCE_SET_2, exportFile.getName(),
                new Long((dateLastModified / 1000) * 1000)));
    }
    if (cookies.length() > 0) {
        // set the cookies, included the session id to keep the same session
        urlcon.setRequestProperty(REQUEST_PROPERTY_COOKIE, cookies.toString());
    }

    // now perform the request
    urlcon.connect();
    int status = urlcon.getResponseCode();

    if (cookies.length() == 0) {
        //Now retrieve the cookies. The jsessionid is here
        cookies.append(urlcon.getHeaderField(HEADER_FIELD_SET_COOKIE));
        if (LOG.isDebugEnabled()) {
            LOG.debug(Messages.get().getBundle().key(Messages.LOG_STATICEXPORT_COOKIES_1, cookies));
        }
    }
    urlcon.disconnect();
    if (LOG.isInfoEnabled()) {
        LOG.info(Messages.get().getBundle().key(Messages.LOG_REQUEST_RESULT_3, rfsName, exportUrlStr,
                new Integer(status)));
    }
    return status;
}

From source file:eionet.cr.harvest.PullHarvest.java

/**
 *
 * @param connectUrl/*w ww . j a  v a 2  s.c  o m*/
 * @return
 * @throws IOException
 * @throws DAOException
 * @throws SAXException
 * @throws ParserConfigurationException
 */
private HttpURLConnection openUrlConnection(String connectUrl)
        throws IOException, DAOException, SAXException, ParserConfigurationException {

    String sanitizedUrl = StringUtils.substringBefore(connectUrl, "#");
    sanitizedUrl = StringUtils.replace(sanitizedUrl, " ", "%20");

    HttpURLConnection connection = (HttpURLConnection) new URL(sanitizedUrl).openConnection();
    connection.setRequestProperty("Accept", ACCEPT_HEADER);
    connection.setRequestProperty("User-Agent", URLUtil.userAgentHeader());
    connection.setRequestProperty("Connection", "close");
    connection.setInstanceFollowRedirects(false);

    // Set the timeout both for establishing the connection, and reading from it once established.
    int httpTimeout = GeneralConfig.getIntProperty(GeneralConfig.HARVESTER_HTTP_TIMEOUT, getTimeout());
    connection.setConnectTimeout(httpTimeout);
    connection.setReadTimeout(httpTimeout);

    // Use "If-Modified-Since" header, if this is not an on-demand harvest
    if (!isOnDemandHarvest) {

        // "If-Modified-Since" will be compared to this URL's last harvest
        Date lastHarvestDate = getContextSourceDTO().getLastHarvest();
        long lastHarvest = lastHarvestDate == null ? 0L : lastHarvestDate.getTime();
        if (lastHarvest > 0) {

            // Check if this URL has a conversion stylesheet, and if the latter has been modified since last harvest.
            String conversionStylesheetUrl = getConversionStylesheetUrl(sanitizedUrl);
            boolean hasConversion = !StringUtils.isBlank(conversionStylesheetUrl);
            boolean hasModifiedConversion = hasConversion
                    && URLUtil.isModifiedSince(conversionStylesheetUrl, lastHarvest);

            // Check if post-harvest scripts are updated
            boolean scriptsModified = DAOFactory.get().getDao(PostHarvestScriptDAO.class)
                    .isScriptsModified(lastHarvestDate, getContextSourceDTO().getUrl());

            // "If-Modified-Since" should only be set if there is no modified conversion or post-harvest scripts for this URL.
            // Because if there is a conversion stylesheet or post-harvest scripts, and any of them has been modified since last
            // harvest, we surely want to get the content again and run the conversion or script on the content, regardless of
            // when the content itself was last modified.
            if (!hasModifiedConversion && !scriptsModified) {
                LOGGER.debug(loggerMsg(
                        "Using if-modified-since, compared to last harvest " + formatDate(lastHarvestDate)));
                connection.setIfModifiedSince(lastHarvest);
            }
        }
    }

    return connection;
}