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:be.solidx.hot.utils.IOUtils.java

public static InputStream loadResourceNoCache(String path) throws IOException {
    URL res = IOUtils.class.getClassLoader().getResource(path);

    if (res == null) {
        throw new IOException("Resource " + path + " does not exists");
    }//ww w. j  av  a 2s  . co  m
    URLConnection resConn = res.openConnection();
    // !!! needed to avoid jvm resource catching
    resConn.setUseCaches(false);
    return resConn.getInputStream();
}

From source file:org.fragzone.unrealmmo.bukkit.entities.npcs.NPCProfile.java

private static void addProperties(GameProfile profile, UUID id) {
    String uuid = id.toString().replaceAll("-", "");

    try {/*from ww w.  j a  v  a2  s . com*/
        // Get the name from SwordPVP
        URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        uc.setDefaultUseCaches(false);
        uc.addRequestProperty("User-Agent", "Mozilla/5.0");
        uc.addRequestProperty("Cache-Control", "no-cache, no-store, must-revalidate");
        uc.addRequestProperty("Pragma", "no-cache");

        // Parse it
        Scanner scanner = new Scanner(uc.getInputStream(), "UTF-8");
        String json = scanner.useDelimiter("\\A").next();
        scanner.close();
        JSONParser parser = new JSONParser();
        Object obj = parser.parse(json);
        JSONArray properties = (JSONArray) ((JSONObject) obj).get("properties");

        for (int i = 0; i < properties.size(); i++) {
            try {
                JSONObject property = (JSONObject) properties.get(i);
                String name = (String) property.get("name");
                String value = (String) property.get("value");
                String signature = property.containsKey("signature") ? (String) property.get("signature")
                        : null;
                if (signature != null) {
                    profile.getProperties().put(name, new Property(name, value, signature));
                } else {
                    profile.getProperties().put(name, new Property(value, name));
                }
            } catch (Exception e) {
                Bukkit.getLogger().log(Level.WARNING, "Failed to apply auth property", e);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 *  This method is used to send a XML request file to web server to process the request and return
 *  xml response containing event id.//from   w w  w.  jav a  2s. c  om
 */
public static String postXMLWithTimeout(String postUrl, String xml, int readTimeout) throws Exception {

    System.out.println("XMLUtils.postXMLWithTimeout: Connecting to Web Server .......");

    InputStream in = null;
    BufferedReader bufferedReader = null;
    OutputStream out = null;
    PrintWriter printWriter = null;
    StringBuffer responseMessageBuffer = new StringBuffer("");

    try {
        URL url = new URL(postUrl);
        URLConnection con = url.openConnection();

        // Prepare for both input and output
        con.setDoInput(true);
        con.setDoOutput(true);

        // Turn off caching
        con.setUseCaches(false);
        con.setRequestProperty("Content-Type", "text/xml");
        con.setReadTimeout(readTimeout);
        out = con.getOutputStream();
        // Write the arguments as post data
        printWriter = new PrintWriter(out);

        printWriter.println(xml);
        printWriter.flush();

        //Process response and return back to caller function
        in = con.getInputStream();
        bufferedReader = new BufferedReader(new InputStreamReader(in));
        String tempStr = null;

        int tempClearResponseMessageBufferCounter = 0;
        while ((tempStr = bufferedReader.readLine()) != null) {
            tempClearResponseMessageBufferCounter++;
            //clear the buffer for the first time
            if (tempClearResponseMessageBufferCounter == 1)
                responseMessageBuffer.setLength(0);
            responseMessageBuffer.append(tempStr);
        }

    } catch (Exception ex) {
        throw ex;
    } finally {
        System.out.println("Calling finally in POSTXML");
        try {
            if (in != null)
                in.close();
        } catch (Exception eex) {
            System.out.println("COULD NOT Close Input Stream in try");
        }
        try {
            if (out != null)
                out.close();
        } catch (Exception eex) {
            System.out.println("COULD NOT Close Output Stream in try");
        }

    }
    System.out.println("XMLUtils.postXMLWithTimeout: end .......");
    return responseMessageBuffer.toString();
}

From source file:com.dianping.resource.io.util.ResourceUtils.java

/**
 * Set the {@link java.net.URLConnection#setUseCaches "useCaches"} flag on the
 * given connection, preferring {@code false} but leaving the
 * flag at {@code true} for JNLP based resources.
 * @param con the URLConnection to set the flag on
 *///from   w  w w.j av  a2  s . c  o  m
public static void useCachesIfNecessary(URLConnection con) {
    con.setUseCaches(con.getClass().getSimpleName().startsWith("JNLP"));
}

From source file:com.opensource.frameworks.processframework.utils.ResourceUtils.java

/**
 * Set the {@link URLConnection#setUseCaches "useCaches"} flag on the
 * given connection, preferring <code>false</code> but leaving the
 * flag at <code>true</code> for JNLP based resources.
 * @param con the URLConnection to set the flag on
 *//*w w  w . ja  v  a2  s . c om*/
public static void useCachesIfNecessary(URLConnection con) {
    con.setUseCaches(con.getClass().getName().startsWith("JNLP"));
}

From source file:org.kuali.rice.devtools.jpa.eclipselink.conv.ojb.OjbUtil.java

private static Object buildRepository(String repositoryFileName, Class targetRepository)
        throws IOException, ParserConfigurationException, SAXException {
    URL url = buildURL(repositoryFileName);

    String pathName = url.toExternalForm();

    LOG.debug("Building repository from :" + pathName);
    InputSource source = new InputSource(pathName);
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false);
    conn.connect();/*w w  w. j  av  a  2 s. co m*/
    InputStream i = conn.getInputStream();
    source.setByteStream(i);
    try {
        return readMetadataFromXML(source, targetRepository);
    } finally {
        try {
            i.close();
        } catch (IOException x) {
            LOG.warn("unable to close repository input stream [" + x.getMessage() + "]", x);
        }
    }
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {/*from   w  w  w.  j  av a2s .  co  m*/
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }
    return null;
}

From source file:flexpos.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {/*from  w  ww .  jav a2s  .  com*/
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(restfulConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:org.gradle.util.GUtil.java

public static Properties loadProperties(URL url) {
    try {//w w  w. ja  va2  s  .  c o  m
        URLConnection uc = url.openConnection();
        uc.setUseCaches(false);
        return loadProperties(uc.getInputStream());
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.impetus.kundera.ejb.PersistenceXmlLoader.java

/**
 * Gets the document./*w ww .  j  a  v a 2  s .c  o m*/
 * 
 * @param configURL
 *            the config url
 * @return the document
 * @throws Exception
 *             the exception
 */
private static Document getDocument(URL configURL) throws Exception {
    InputStream is = null;
    if (configURL != null) {
        URLConnection conn = configURL.openConnection();
        conn.setUseCaches(false); // avoid JAR locking on Windows and Tomcat
        is = conn.getInputStream();
    }
    if (is == null) {
        throw new IOException("Failed to obtain InputStream from url: " + configURL);
    }

    DocumentBuilderFactory docBuilderFactory = null;
    docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setValidating(true);
    docBuilderFactory.setNamespaceAware(true);

    try {
        // otherwise Xerces fails in validation
        docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true);
    } catch (IllegalArgumentException e) {
        docBuilderFactory.setValidating(false);
        docBuilderFactory.setNamespaceAware(false);
    }

    InputSource source = new InputSource(is);
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    // docBuilder.setEntityResolver( resolver );

    List errors = new ArrayList();
    docBuilder.setErrorHandler(new ErrorLogger("XML InputStream", errors));
    Document doc = docBuilder.parse(source);

    if (errors.size() != 0) {
        throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0));
    }
    is.close(); //Close input Stream
    return doc;
}