private InputStream sink(InputStream in) throws IOException {
   File f = File.createTempFile("mylitte", "jar");
   LOG.debug("Write: " + f.getAbsolutePath());
   FileOutputStream fout = new FileOutputStream(f);
   StreamUtils.copyStream(in, fout, true);
   return new FileInputStream(f);
 }
  /**
   * Returns the input stream denoted by the url.
   *
   * @return the input stream for the resource denoted by url
   * @throws java.io.IOException in case of an exception during accessing the resource
   * @see java.net.URLConnection#getInputStream()
   */
  @Override
  public InputStream getInputStream() throws IOException {
    connect();
    final File workingDir = m_configuration.getWorkingDirectory();
    final File cacheMetaFile = new File(workingDir, m_cacheName + EXT_META);
    final File cacheDateFile = new File(workingDir, m_cacheName + EXT_DATA);

    final Properties cacheMeta = new Properties();
    try {
      InputStream in = new FileInputStream(cacheMetaFile);
      try {
        cacheMeta.load(in);
      } finally {
        in.close();
      }
    } catch (FileNotFoundException ignore) {
      // ignore
    }

    final String cacheUrl = cacheMeta.getProperty(META_URL);
    if (cacheUrl == null) {
      cacheMeta.setProperty(META_URL, url.getPath());
    }

    final String cacheTime = cacheMeta.getProperty(META_CACHED_ON);
    if (cacheTime == null || !cacheDateFile.exists()) {
      StreamUtils.copyStream(
          m_parser.getUrl().openStream(),
          new BufferedOutputStream(new FileOutputStream(cacheDateFile)),
          true);
      cacheMeta.setProperty(META_CACHED_ON, String.valueOf(System.currentTimeMillis()));
    }

    OutputStream out = new FileOutputStream(cacheMetaFile);
    try {
      cacheMeta.store(out, null);
    } finally {
      out.close();
    }
    return new BufferedInputStream(new FileInputStream(cacheDateFile));
  }
Пример #3
0
  @Test
  public void runStaticResourceServlet() throws Exception {
    URL url = new URL("https://localhost:8443/sample1/hello");
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    SSLContext ssl = SSLContext.getInstance("TLS");
    ssl.init(null, TRUST_ALL_CERTS, null);

    con.setSSLSocketFactory(ssl.getSocketFactory());
    con.setHostnameVerifier(
        new HostnameVerifier() {
          public boolean verify(String s, SSLSession sslSession) {
            return true;
          }
        });

    assertThat(servletContext.getContextPath(), is("/sample1"));
    InputStream is = con.getInputStream();
    OutputStream os = new ByteArrayOutputStream();
    StreamUtils.copyStream(is, os, true);
    assertThat(os.toString(), containsString("Hello from Pax Web!"));
  }