Example usage for java.net URLConnection setUseCaches

List of usage examples for java.net URLConnection setUseCaches

Introduction

In this page you can find the example usage for java.net URLConnection setUseCaches.

Prototype

public void setUseCaches(boolean usecaches) 

Source Link

Document

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

Usage

From source file:com.wavemaker.runtime.ws.SyndFeedService.java

/**
 * Reads from the InputStream of the specified URL and builds the feed object from the returned XML.
 * /*w  ww. ja  v a2 s.  co  m*/
 * @param feedURL The URL to read feed from.
 * @param httpBasicAuthUsername The username for HTTP Basic Authentication.
 * @param httpBasicAuthPassword The password for HTTP Basic Authentication.
 * @param connectionTimeout HTTP connection timeout.
 * @return A feed object.
 */
@ExposeToClient
public Feed getFeedWithHttpConfig(String feedURL, String httpBasicAuthUsername, String httpBasicAuthPassword,
        int connectionTimeout) {
    URL url = null;
    try {
        url = new URL(feedURL);
    } catch (MalformedURLException e) {
        throw new WebServiceInvocationException(e);
    }

    SyndFeedInput input = new SyndFeedInput();
    try {
        URLConnection urlConn = url.openConnection();
        if (urlConn instanceof HttpURLConnection) {
            urlConn.setAllowUserInteraction(false);
            urlConn.setDoInput(true);
            urlConn.setDoOutput(false);
            ((HttpURLConnection) urlConn).setInstanceFollowRedirects(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestProperty(USER_AGENT_KEY, USER_AGENT_VALUE);

            urlConn.setConnectTimeout(connectionTimeout);

            if (httpBasicAuthUsername != null && httpBasicAuthUsername.length() > 0) {
                String auth = httpBasicAuthPassword == null ? httpBasicAuthUsername
                        : httpBasicAuthUsername + ":" + httpBasicAuthPassword;
                urlConn.setRequestProperty(BASIC_AUTH_KEY,
                        BASIC_AUTH_VALUE_PREFIX + Base64.encodeBase64URLSafeString(auth.getBytes()));
            }
        }
        SyndFeed feed = input.build(new XmlReader(urlConn));
        return FeedBuilder.getFeed(feed);
    } catch (IllegalArgumentException e) {
        throw new WebServiceInvocationException(e);
    } catch (FeedException e) {
        throw new WebServiceInvocationException(e);
    } catch (IOException e) {
        throw new WebServiceInvocationException(e);
    }
}

From source file:org.jab.docsearch.utils.NetUtils.java

/**
 * Downloads URL to file/*  ww w.j  a v a 2  s  .  c  o m*/
 *
 * Content type, content length, last modified and md5 will be set in SpiderUrl
 *
 * @param spiderUrl  URL to download
 * @param file       URL content downloads to this file
 * @return           true if URL successfully downloads to a file
 */
public boolean downloadURLToFile(final SpiderUrl spiderUrl, final String file) {

    boolean downloadOk = false;
    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        // if the file downloads - save it and return true
        URL url = new URL(spiderUrl.getUrl());
        URLConnection conn = url.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        // content type
        spiderUrl.setContentType(getContentType(conn));

        // open streams
        inputStream = new BufferedInputStream(conn.getInputStream());
        outputStream = new BufferedOutputStream(new FileOutputStream(file));

        // copy data from URL to file
        long size = 0;
        int readed = 0;
        while ((readed = inputStream.read()) != -1) {
            size++;
            outputStream.write(readed);
        }

        // set values
        spiderUrl.setContentType(getContentType(conn));
        spiderUrl.setSize(size);

        downloadOk = true;
    } catch (IOException ioe) {
        logger.fatal("downloadURLToFile() failed for URL='" + spiderUrl.getUrl() + "'", ioe);
        downloadOk = false;
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }

    // set values
    if (downloadOk) {
        spiderUrl.setMd5(FileUtils.getMD5Sum(file));
    }

    return downloadOk;
}

From source file:org.squale.welcom.outils.WelcomConfigurator.java

/**
 * Initialise les proprits du fichier de configuration - lastDate - isGoodResources
 * //from ww w  .j  a va 2  s  . c  o  m
 * @param pFileName fichier de configuration
 */
private void initializeFile(final String pFileName) {

    String realFileName = "";

    this.fileName = pFileName;

    InputStream is = null;

    if (!GenericValidator.isBlankOrNull(fileName)) {

        // Set up to load the property resource for this locale key, if we can
        realFileName = fileName.replace('.', '/') + ".properties";

        goodFileResources = true;
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

        // Recherche la date de derniere modification
        final URL url = classLoader.getResource(realFileName);
        URLConnection urlConn;
        try {
            urlConn = url.openConnection();
            urlConn.setUseCaches(false);
            urlConn.connect();
            urlConn.getLastModified();
            lastDateFile = new Date(urlConn.getLastModified());
        } catch (final Exception e) {
            goodFileResources = false;
        }

        // Regarde si le fichier n'est pas vide
        is = classLoader.getResourceAsStream(realFileName);
        goodFileResources = (is != null);
    }

}

From source file:org.phaidra.apihooks.APIRESTHooksImpl.java

/**
 * Runs the hook if enabled in fedora.fcfg.
 *
 * @param method The name of the method that calls the hook
 * @param pid The PID that is being accessed
 * @param params Method parameters, depend on the method called
 * @return String Hook verdict. Begins with "OK" if it's ok to proceed.
 * @throws APIHooksException If the remote call went wrong
 *///from   w ww.  ja  v  a2s  . c o  m
public String runHook(String method, DOWriter w, Context context, String pid, Object[] params)
        throws APIHooksException {
    String rval = null;

    // Only do this if the method is enabled in fedora.fcfg
    if (getParameter(method) == null) {
        log.debug("runHook: method |" + method + "| not configured, not calling webservice");
        return "OK";
    }

    Iterator i = context.subjectAttributes();
    String attrs = "";
    while (i.hasNext()) {
        String name = "";
        try {
            name = (String) i.next();
            String[] value = context.getSubjectValues(name);
            for (int j = 0; j < value.length; j++) {
                attrs += "&attr=" + URLEncoder.encode(name + "=" + value[j], "UTF-8");
                log.debug("runHook: will send |" + name + "=" + value[j] + "| as subject attribute");
            }
        } catch (NullPointerException ex) {
            log.debug(
                    "runHook: caught NullPointerException while trying to retrieve subject attribute " + name);
        } catch (UnsupportedEncodingException ex) {
            log.debug("runHook: caught UnsupportedEncodingException while trying to encode subject attribute "
                    + name);
        }
    }
    String paramstr = "";
    try {
        for (int j = 0; j < params.length; j++) {
            paramstr += "&param" + Integer.toString(j) + "=";
            if (params[j] != null) {
                String p = params[j].toString();
                paramstr += URLEncoder.encode(p, "UTF-8");
            }
        }
    } catch (UnsupportedEncodingException ex) {
        log.debug("runHook: caught UnsupportedEncodingException while trying to encode a parameter");
    }

    String loginId = context.getSubjectValue(Constants.SUBJECT.LOGIN_ID.uri);

    log.debug("runHook: called for method=|" + method + "|, pid=|" + pid + "|");
    try {
        // TODO: timeout? retries?
        URL url;
        URLConnection urlConn;
        DataOutputStream printout;
        BufferedReader input;

        url = new URL(getParameter("restmethod"));
        urlConn = url.openConnection();
        urlConn.setDoInput(true);
        urlConn.setDoOutput(true);
        urlConn.setUseCaches(false);
        urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        printout = new DataOutputStream(urlConn.getOutputStream());
        String content = "method=" + URLEncoder.encode(method, "UTF-8") + "&username="
                + URLEncoder.encode(loginId, "UTF-8") + "&pid=" + URLEncoder.encode(pid, "UTF-8") + paramstr
                + attrs;
        printout.writeBytes(content);
        printout.flush();
        printout.close();

        // Get response data.
        input = new BufferedReader(new InputStreamReader(urlConn.getInputStream(), "UTF-8"));
        String str;
        rval = "";
        while (null != ((str = input.readLine()))) {
            rval += str + "\n";
        }
        input.close();

        String ct = urlConn.getContentType();
        if (ct.startsWith("text/xml")) {
            log.debug("runHook: successful REST invocation for method |" + method + "|, returning: " + rval);
        } else if (ct.startsWith("text/plain")) {
            log.debug("runHook: successful REST invocation for method |" + method
                    + "|, but hook returned an error: " + rval);
            throw new Exception(rval);
        } else {
            throw new Exception("Invalid content type " + ct);
        }
    } catch (Exception ex) {
        log.error("runHook: error calling REST hook: " + ex.toString());
        throw new APIHooksException("Error calling REST hook: " + ex.toString(), ex);
    }

    return processResults(rval, w, context);
}

From source file:org.jab.docsearch.utils.NetUtils.java

/**
 * Get SpiderUrl status/*from w w  w .  j a v  a  2 s . c  o  m*/
 *
 * Content type, content length, last modified and md5 will be set in SpiderUrl if url is changed
 *
 * @param spiderUrl  URL to check
 * @param file       URL content downloads to to this file
 * @return           -1 if link is broken, 0 if the file is unchanged or 1 if the file
 *                   is different... part of caching algoritm.
 */
public int getURLStatus(final SpiderUrl spiderUrl, final String file) {
    // -1 means broken link
    //  0 means same file
    //  1 means changed

    int status;

    try {
        // attempt to obtain status from date
        URL url = new URL(spiderUrl.getUrl());
        URLConnection conn = url.openConnection();

        // set connection parameter
        conn.setDoInput(true);
        conn.setDoOutput(false);
        conn.setUseCaches(false);
        conn.setRequestProperty("User-Agent", USER_AGENT);

        // connect
        conn.connect();

        // content type
        spiderUrl.setContentType(getContentType(conn));

        // check date
        long spiDate = spiderUrl.getLastModified();
        long urlDate = conn.getLastModified();

        // same if date is equal and not zero
        if (spiDate == urlDate && urlDate != 0) {
            // the file is not changed
            status = 0;
        } else {
            // download the URL and compare hashes
            boolean downloaded = downloadURLToFile(conn, file);
            if (downloaded) {
                // download ok

                // compare file hashes
                String fileHash = FileUtils.getMD5Sum(file);

                if (fileHash.equals(spiderUrl.getMd5())) {
                    // same
                    status = 0;
                } else {
                    // changed
                    status = 1;

                    // set changed values
                    spiderUrl.setSize(FileUtils.getFileSize(file));
                    spiderUrl.setLastModified(urlDate);
                    spiderUrl.setMd5(fileHash);
                }
            } else {
                // download failed

                // broken link
                status = -1;
            }
        }
    } catch (IOException ioe) {
        logger.error("getURLStatus() failed for URL='" + spiderUrl.getUrl() + "'", ioe);
        status = -1;
    }

    return status;
}

From source file:spring.osgi.io.OsgiBundleResource.java

/**
 * Returns an <code>InputStream</code> to this resource. This
 * implementation opens an//from   ww w.  j  a  va  2s  .  co  m
 * <code>InputStream<code> for the given <code>URL</code>. It sets the
 * <em>UseCaches</em> flag to <code>false</code>, mainly to avoid jar file
 * locking on Windows.
 *
 * @return input stream to the underlying resource
 * @throws java.io.IOException if the stream could not be opened
 * @see java.net.URL#openConnection()
 * @see java.net.URLConnection#setUseCaches(boolean)
 * @see java.net.URLConnection#getInputStream()
 */
public InputStream getInputStream() throws IOException {
    URLConnection con = getURL().openConnection();
    con.setUseCaches(false);
    return con.getInputStream();
}

From source file:org.impalaframework.web.servlet.ResourceServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String rawResourcePath = rawResourcePath(request);

    if (log.isDebugEnabled()) {
        log.debug("Attempting to GET resource: " + rawResourcePath);
    }/*w  w w.  j a va2s .co m*/

    URL[] resources = getRequestResourceURLs(request);

    if (resources == null || resources.length == 0) {
        if (log.isDebugEnabled()) {
            log.debug("Resource not found: " + rawResourcePath);
        }
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

    prepareResponse(response, resources, rawResourcePath);

    OutputStream out = selectOutputStream(request, response);

    try {
        for (int i = 0; i < resources.length; i++) {
            URLConnection resourceConn = resources[i].openConnection();
            resourceConn.setUseCaches(false);
            InputStream in = resourceConn.getInputStream();
            try {
                byte[] buffer = new byte[1024];
                int bytesRead = -1;
                while ((bytesRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                in.close();
            }
        }
    } finally {
        out.close();
    }
}

From source file:net.solarnetwork.node.io.url.UrlDataCollector.java

@Override
public void collectData() {
    String resolvedUrl = url;/* w  w  w .j  a  v a 2 s .co  m*/
    if (urlFactory != null) {
        resolvedUrl = urlFactory.getObject();
    }
    URL dataUrl = null;
    try {
        dataUrl = new URL(resolvedUrl);
    } catch (MalformedURLException e) {
        throw new RuntimeException("Bad url configured: " + resolvedUrl);
    }
    if (log.isDebugEnabled()) {
        log.debug("Connecting to URL [" + resolvedUrl + ']');
    }
    BufferedReader reader = null;
    String data = null;
    String enc = null;
    Pattern pat = Pattern.compile(matchExpression);
    try {
        URLConnection conn = dataUrl.openConnection();
        conn.setConnectTimeout(connectionTimeout);
        conn.setReadTimeout(connectionTimeout);
        conn.setUseCaches(false);
        InputStream in = conn.getInputStream();
        if (this.encoding == null) {
            enc = conn.getContentEncoding();
            if (enc != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Using connection encoding [" + enc + ']');
                }
                this.encoding = enc;
            }
        }
        if (enc == null) {
            enc = getEncodingToUse();
        }
        reader = new BufferedReader(new InputStreamReader(in, enc));
        String lastLine = null;
        boolean keepGoing = true;
        while (keepGoing) {
            String line = reader.readLine();
            if (line == null) {
                keepGoing = false;
                if (skipToLastLine) {
                    line = lastLine;
                }
            }
            Matcher m = pat.matcher(line);
            if (m.find()) {
                if (log.isDebugEnabled()) {
                    log.debug("Found matching data line [" + line + ']');
                }
                data = line;
                keepGoing = false;
            } else {
                lastLine = line;
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                if (log.isWarnEnabled()) {
                    log.warn("IOException closing input stream: " + e);
                }
            }
        }
    }
    if (data == null) {
        log.info("Input stream finished without finding expected data");
    } else {
        if (this.buffer == null) {
            this.buffer = new StringBuilder(data);
        } else {
            this.buffer.append(data);
        }
    }
}

From source file:spring.osgi.io.OsgiBundleResource.java

public long lastModified() throws IOException {
    URLConnection con = getURL().openConnection();
    con.setUseCaches(false);
    long time = con.getLastModified();
    // the implementation doesn't return the proper time stamp
    if (time == 0) {
        if (OsgiResourceUtils.PREFIX_TYPE_BUNDLE_JAR == searchType)
            return bundle.getLastModified();
    }//  www .j av a 2 s . c o m
    // there is nothing else we can do
    return time;
}

From source file:fr.gael.drb.impl.http.HttpNode.java

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override/*from w  w w.j a va 2s.c  o m*/
public Object getImpl(Class api) {
    if (api.isAssignableFrom(InputStream.class)) {
        try {
            URLConnection urlConnect = this.url.openConnection();

            if (this.url.getUserInfo() != null) {
                // HTTP Basic Authentication.
                String userpass = this.url.getUserInfo();
                String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
                urlConnect.setRequestProperty("Authorization", basicAuth);
            }

            urlConnect.setDoInput(true);
            urlConnect.setUseCaches(false);

            return urlConnect.getInputStream();
        } catch (IOException e) {
            return null;
        }
    }
    return null;
}