List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:org.jab.docsearch.spider.LinkFinder.java
/** * Get all links from page/*from w w w . ja v a2s .c o m*/ */ public void getAllLinks() { // writes links from a page out to a file String urlStr = pageName; String shortUrl = ""; numUnChanged = 0; numSkips = 0; int numSuccesses = 0; int numFailed = 0; int numNoRobots = 0; addLink(urlStr); domainUrl = Utils.getDomainURL(urlStr); if (logger.isDebugEnabled()) { logger.debug("getAllLinks() domain url='" + domainUrl + "'"); } SpiderUrl curl = new SpiderUrl(urlStr); baseUrlFolder = Utils.getBaseURLFolder(urlStr); int curLinkNo = 0; boolean completedSpider = false; boolean isDead = false; int curPread = 0; if (ds != null) { ds.setIsWorking(true); ds.setProgressMax(maxLinksToFind); ds.setCurProgressMSG("Spidering Files..."); } int numSpidered = 0; int curSuccessNo = 0; // start spider while (curLinkNo != -1) { BufferedInputStream urlStream = null; FileOutputStream fileOutStream = null; try { completedSpider = false; isDead = false; if (ds != null) { ds.setCurProgress(curPread); if (!ds.getIsWorking()) { break; } } curLinkNo = getNextUrlNo(); if (curLinkNo == -1) { logger.debug("getAllLinks() end of links reached."); break; } else { urlStr = getLinkNameByNo(curLinkNo); logger.info("getAllLinks() analyzing page='" + urlStr + "'"); curl = getSpiderUrl(curLinkNo); } shortUrl = Utils.concatEnd(urlStr, 33); setStatus(I18n.getString("connecting_to") + " " + shortUrl); // open url URL url = new URL(urlStr); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); conn.setRequestProperty("User-Agent", "DocSearcher " + I18n.getString("ds.version")); conn.connect(); urlStream = new BufferedInputStream(conn.getInputStream()); // filesize int fileSize = conn.getContentLength(); if (fileSize > maxFileSizeToGet) { String ex = I18n.getString("skipping_file_too_big") + " (" + fileSize + " > " + maxFileSizeToGet + ") " + shortUrl; setStatus(ex); throw new Exception(ex); } setStatus(I18n.getString("downloading_uc") + "... " + shortUrl + " " + fileSize + " " + I18n.getString("bytes")); curl.setSize(fileSize); // last modified long curModified = conn.getLastModified(); // was .getDate(); curl.setLastModified(curModified); // content type String curContentType = netUtils.getContentType(conn); curl.setContentType(curContentType); // build the value for downloadFile String dnldTmpName = getDownloadFileName(curl.getContentType(), urlStr.toLowerCase()); String downloadFile = FileUtils.addFolder(downloadFileDir, dnldTmpName); // TODO it is better to use content type! boolean curIsWebPage = isHtml(urlStr.toLowerCase()) || (curContentType.toLowerCase().indexOf("html") != -1); logger.debug("getAllLinks() saving to " + downloadFile); fileOutStream = new FileOutputStream(downloadFile); int curSize = 0; int curI; int lastPercent = 0; StringBuilder tag = new StringBuilder(); String link = null; boolean inTag = false; boolean getFileSizeFromStream = false; if (fileSize == -1) { getFileSizeFromStream = true; } while ((curI = urlStream.read()) != -1) { fileOutStream.write(curI); curSize++; if (ds != null) { if (!ds.getIsWorking()) { break; } } // fix problem if filesize not in content length if (getFileSizeFromStream) { fileSize = curSize + urlStream.available(); } // notify of download progress if (curSize > 0 && (curSize % 10) == 0) { int curPercent = (curSize * 100) / fileSize; if (curPercent != lastPercent) { lastPercent = curPercent; setStatus(I18n.getString("downloading_uc") + "... : (" + shortUrl + ") --> " + curPercent + " %" + " ( " + (numSuccesses + numFailed + numNoRobots) + "/" + getNumLinksFound() + ")"); } } // end for percent updates else if (curSize % 40 == 0) { setStatus(I18n.getString("downloading_uc") + "... : (" + shortUrl + ") --> " + curSize + " " + I18n.getString("bytes")); } // handle links if (curIsWebPage) { char c = (char) curI; // LOOK AT THE TAGS // start tag if (c == '<') { inTag = true; tag = new StringBuilder(); } // end tag else if (c == '>') { inTag = false; tag.append(c); String realTag = tag.toString(); String lowerTag = realTag.toLowerCase(); // TODO fix problem with spaces before = // link if (lowerTag.startsWith("<a ")) { link = Utils.getTagString("href=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } // area else if (lowerTag.startsWith("<area")) { link = Utils.getTagString("href=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } // TODO is in param realy a link? else if (lowerTag.startsWith("<param")) { String appletParam = Utils.getTagString("name=", realTag); if (appletParam.toLowerCase().equals("url")) { link = Utils.getTagString("value=", realTag); link = Utils.getNormalUrl(link); doPossibleAdd(urlStr, link); } } } // in tag if (inTag) { tag.append(c); } } // filesize ok if (getFileSizeFromStream && fileSize > maxFileSizeToGet) { break; } } // end while downloading curPread++; fileOutStream.close(); urlStream.close(); curl.setMd5(FileUtils.getMD5Sum(downloadFile)); // now add out document if (ds != null) { curSuccessNo = ds.idx.addDocToIndex(downloadFile, iw, dsi, false, curl); switch (curSuccessNo) { case 0: // good numSuccesses++; break; case 1: // bad numFailed++; break; case 2: // meta robots - no index numNoRobots++; break; } } // delete temp file if (!FileUtils.deleteFile(downloadFile)) { logger.warn("getAllLinks() can't delete file '" + downloadFile + "'"); } numSpidered++; completedSpider = true; // max links found if (numSpidered > maxLinksToFind) { break; } } catch (Exception e) { logger.fatal("getAllLinks() failed", e); setStatus(I18n.getString("error") + " : " + e.toString()); isDead = true; } finally { // close resources IOUtils.closeQuietly(urlStream); IOUtils.closeQuietly(fileOutStream); curl.setSpidered(completedSpider); curl.setIsDeadLink(isDead); setStatus(I18n.getString("download_complete") + " " + shortUrl); } } // end for iterating over links if (ds != null) { ds.resetProgress(); } saveAllLinks(); logger.info("getAllLinks() " + numSpidered + " total web pages spidered for links."); showMessage(I18n.getString("spidering_complete") + " (" + Utils.concatStrToEnd(pageName, 28) + ") ", numSpidered + " " + I18n.getString("documents_indexed") + " " + getNumLinksFound() + " " + I18n.getString("links_found") + "\n\n" + numSuccesses + " " + I18n.getString("documents_spidered_successful") + "\n\n" + numFailed + " " + I18n.getString("documents_spidered_failed") + "\n\n" + numNoRobots + " " + I18n.getString("documents_not_spidered")); }
From source file:webServices.RestServiceImpl.java
@POST @Path("/downloadFile/") @Consumes({ MediaType.TEXT_PLAIN })/*from w ww.j a v a 2 s .c o m*/ @Produces({ MediaType.TEXT_PLAIN }) public String startDownload(String aurl) { int count; String outName = null; // generate folder for kml files File dir = new File(context.getRealPath("/") + STORE_FOLDER); dir.mkdir(); try { String fileName = aurl.substring(aurl.lastIndexOf('/') + 1); if (PROXY_STATUS.equalsIgnoreCase("on")) { outName = HTTP + PROXY_NAME + ":" + PROXY_PORT + context.getContextPath() + "/" + STORE_FOLDER + fileName; } else { outName = HTTP + servlet.getServerName() + ":" + servlet.getServerPort() + context.getContextPath() + "/" + STORE_FOLDER + fileName; } URL url = new URL(aurl); URLConnection conexion = url.openConnection(); conexion.connect(); InputStream input = new BufferedInputStream(url.openStream()); OutputStream output = new FileOutputStream( new File(context.getRealPath("/") + STORE_FOLDER + fileName)); byte data[] = new byte[1024]; while ((count = input.read(data)) != -1) { output.write(data, 0, count); } output.flush(); output.close(); input.close(); } catch (Exception e) { } return (outName); }
From source file:com.blackducksoftware.integration.hub.cli.CLIDownloadService.java
public void customInstall(HubProxyInfo hubProxyInfo, CLILocation cliLocation, CIEnvironmentVariables ciEnvironmentVariables, final URL archive, String hubVersion, final String localHostName) throws IOException, InterruptedException, HubIntegrationException, IllegalArgumentException, EncryptionException { boolean cliMismatch = true; try {/*from w w w . java2 s. c om*/ final File hubVersionFile = cliLocation.createHubVersionFile(); if (hubVersionFile.exists()) { final String storedHubVersion = IOUtils.toString(new FileReader(hubVersionFile)); if (hubVersion.equals(storedHubVersion)) { cliMismatch = false; } else { hubVersionFile.delete(); hubVersionFile.createNewFile(); } } final File cliInstallDirectory = cliLocation.getCLIInstallDir(); if (!cliInstallDirectory.exists()) { cliMismatch = true; } if (cliMismatch) { logger.debug("Attempting to download the Hub CLI."); final FileWriter writer = new FileWriter(hubVersionFile); writer.write(hubVersion); writer.close(); hubVersionFile.setLastModified(0L); } final long cliTimestamp = hubVersionFile.lastModified(); URLConnection connection = null; try { Proxy proxy = null; if (hubProxyInfo != null) { String proxyHost = hubProxyInfo.getHost(); int proxyPort = hubProxyInfo.getPort(); String proxyUsername = hubProxyInfo.getUsername(); String proxyPassword = hubProxyInfo.getDecryptedPassword(); if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) { proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); } if (proxy != null) { if (StringUtils.isNotBlank(proxyUsername) && StringUtils.isNotBlank(proxyPassword)) { AuthenticatorUtil.setAuthenticator(proxyUsername, proxyPassword); } else { AuthenticatorUtil.resetAuthenticator(); } } } if (proxy != null) { connection = archive.openConnection(proxy); } else { connection = archive.openConnection(); } connection.setIfModifiedSince(cliTimestamp); connection.connect(); } catch (final IOException ioe) { logger.error("Skipping installation of " + archive + " to " + cliLocation.getCanonicalPath() + ": " + ioe.toString()); return; } if (connection instanceof HttpURLConnection && ((HttpURLConnection) connection).getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { // CLI has not been modified return; } final long sourceTimestamp = connection.getLastModified(); if (cliInstallDirectory.exists() && cliInstallDirectory.listFiles().length > 0) { if (!cliMismatch && sourceTimestamp == cliTimestamp) { logger.debug("The current Hub CLI is up to date."); return; } for (final File file : cliInstallDirectory.listFiles()) { FileUtils.deleteDirectory(file); } } else { cliInstallDirectory.mkdir(); } logger.debug("Updating the Hub CLI."); hubVersionFile.setLastModified(sourceTimestamp); logger.info("Unpacking " + archive.toString() + " to " + cliInstallDirectory.getCanonicalPath() + " on " + localHostName); final CountingInputStream cis = new CountingInputStream(connection.getInputStream()); try { unzip(cliInstallDirectory, cis, logger); updateJreSecurity(logger, cliLocation, ciEnvironmentVariables); } catch (final IOException e) { throw new IOException(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), connection.getContentLength()), e); } } catch (final IOException e) { throw new IOException("Failed to install " + archive + " to " + cliLocation.getCanonicalPath(), e); } }
From source file:org.sakaiproject.calendar.impl.BaseExternalCalendarSubscriptionService.java
ExternalSubscription loadCalendarSubscriptionFromUrl(String url, String context, String calendarName, String forcedEventType) { ExternalSubscription subscription = new BaseExternalSubscription(calendarName, url, context, null, INSTITUTIONAL_CONTEXT.equals(context)); ExternalCalendarSubscription calendar = null; List<CalendarEvent> events = null; BufferedInputStream stream = null; try {//from ww w. j a v a 2 s .com URL _url = new URL(url); if (calendarName == null) calendarName = _url.getFile(); // connect URLConnection conn = _url.openConnection(); // Must set user agent so we can detect loops. conn.addRequestProperty("User-Agent", m_calendarService.getUserAgent()); conn.setConnectTimeout(TIMEOUT); conn.setReadTimeout(TIMEOUT); // Now make the connection. conn.connect(); stream = new BufferedInputStream(conn.getInputStream()); // import events = m_importerService.doImport(CalendarImporterService.ICALENDAR_IMPORT, stream, columnMap, null); String subscriptionId = getIdFromSubscriptionUrl(url); String reference = calendarSubscriptionReference(context, subscriptionId); calendar = new ExternalCalendarSubscription(reference); for (CalendarEvent event : events) { String eventType = event.getType(); if (forcedEventType != null) eventType = forcedEventType; calendar.addEvent(event.getRange(), event.getDisplayName(), event.getDescription(), eventType, event.getLocation(), event.getRecurrenceRule(), null); } calendar.setName(calendarName); subscription.setCalendar(calendar); subscription.setInstitutional(getInstitutionalSubscription(url) != null); m_log.info("Loaded calendar subscription: " + subscription.toString()); } catch (ImportException e) { m_log.info("Error loading calendar subscription '" + calendarName + "' (will NOT retry again): " + url); String subscriptionId = getIdFromSubscriptionUrl(url); String reference = calendarSubscriptionReference(context, subscriptionId); calendar = new ExternalCalendarSubscription(reference); calendar.setName(calendarName); // By setting the calendar to be an empty one we make sure that we don't attempt to re-retrieve it // When 2 hours are up it will get refreshed through. subscription.setCalendar(calendar); } catch (PermissionException e) { // This will never be called (for now) e.printStackTrace(); } catch (MalformedURLException e) { m_log.info("Mal-formed URL in calendar subscription '" + calendarName + "': " + url); } catch (IOException e) { m_log.info("Unable to read calendar subscription '" + calendarName + "' from URL (I/O Error): " + url); } catch (Exception e) { m_log.info("Unknown error occurred while reading calendar subscription '" + calendarName + "' from URL: " + url); } finally { if (stream != null) { // Also closes the underlying InputStream try { stream.close(); } catch (IOException e) { // Ignore } } } return subscription; }
From source file:com.adito.extensions.store.ExtensionStore.java
/** * @param id//from ww w . j a va2 s .co m * @param version * @return URLConnection * @throws IOException */ public URLConnection downloadExtension(String id, String version) throws IOException { URL downloadURL = getDownloadURL(id, version); if (downloadURL != null) { if (log.isInfoEnabled()) log.info("Downloading extension from " + downloadURL.toExternalForm()); URLConnection con = downloadURL.openConnection(); con.setConnectTimeout(CONNECT_TIMEOUT); con.setReadTimeout(READ_TIMEOUT); con.connect(); return con; } else { throw new IOException("No valid download location for " + id); } }
From source file:org.wso2.carbon.mediation.registry.MicroIntegratorRegistry.java
@Override public OMNode lookup(String key) { if (log.isDebugEnabled()) { log.debug("==> Repository fetch of resource with key : " + key); }/* w w w. ja v a 2s. co m*/ String resolvedRegKeyPath = resolveRegistryPath(key); URLConnection urlConnection; URL url = null; try { url = new URL(resolvedRegKeyPath); } catch (MalformedURLException e) { handleException("Invalid path '" + resolvedRegKeyPath + "' for URL", e); } if (url == null) { handleException("Unable to create URL for target resource : " + key); } if ("file".equals(url.getProtocol())) { try { url.openStream(); } catch (IOException e) { if (log.isDebugEnabled()) { log.debug("Error occurred while accessing registry resource: " + key, e); } return null; } } try { urlConnection = url.openConnection(); urlConnection.connect(); } catch (IOException e) { return null; } InputStream input = null; try { input = urlConnection.getInputStream(); } catch (IOException e) { handleException("Error when getting a stream from the URL", e); } if (input == null) { return null; } BufferedInputStream inputStream = new BufferedInputStream(input); OMNode result = null; try { XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); StAXOMBuilder builder = new StAXOMBuilder(parser); result = builder.getDocumentElement(); } catch (OMException | XMLStreamException ignored) { if (log.isDebugEnabled()) { log.debug("The resource at the provided URL isn't well-formed XML,So,takes it as a text"); } try { inputStream.close(); } catch (IOException e) { log.error("Error in closing the input stream. ", e); } try { result = readNonXML(url); } catch (IOException e) { log.error("Error occurred while retrieving text content from registry artifact", e); result = null; } } finally { try { if (result != null && result.getParent() != null) { result.detach(); OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory()); parent.addChild(result); } inputStream.close(); } catch (IOException e) { log.error("Error in closing the input stream.", e); } } return result; }
From source file:org.apache.hadoop.yarn.client.cli.TopCLI.java
private URLConnection connect(URL url) throws Exception { AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL authUrl;/*from w ww . java 2s . co m*/ SSLFactory clientSslFactory; URLConnection connection; // If https is chosen, configures SSL client. if (YarnConfiguration.useHttps(getConf())) { clientSslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, getConf()); clientSslFactory.init(); SSLSocketFactory sslSocktFact = clientSslFactory.createSSLSocketFactory(); authUrl = new AuthenticatedURL(new KerberosAuthenticator(), clientSslFactory); connection = authUrl.openConnection(url, token); HttpsURLConnection httpsConn = (HttpsURLConnection) connection; httpsConn.setSSLSocketFactory(sslSocktFact); } else { authUrl = new AuthenticatedURL(new KerberosAuthenticator()); connection = authUrl.openConnection(url, token); } connection.connect(); return connection; }
From source file:org.belio.service.gateway.AirtelCharging.java
private String sendXmlOverPost(String url, String xml) { StringBuffer result = new StringBuffer(); try {/*from ww w . ja v a 2s . c o m*/ Launcher.LOG.info("======================xml=============="); Launcher.LOG.info(xml.toString()); // String userPassword = "roamtech_KE:roamtech _KE!ibm123"; // URL url2 = new URL("https://41.223.58.133:8443/ChargingServiceFlowWeb/sca/ChargingExport1"); String userPassword = "" + networkproperties.getProperty("air_info_u") + ":" + networkproperties.getProperty("air_info_p"); URL url2 = new URL(url); // URLConnection urlc = url.openConnection(); URLConnection urlc = url2.openConnection(); HttpsURLConnection conn = (HttpsURLConnection) urlc; conn.setDoInput(true); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.setRequestProperty("SOAPAction", "run"); // urlc.setDoOutput(true); // urlc.setUseCaches(false); // urlc.setAllowUserInteraction(false); conn.setRequestProperty("Authorization", "Basic " + new Base64().encode(userPassword.getBytes())); conn.setRequestProperty("Content-Type", "text/xml"); conn.setHostnameVerifier(new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }); // Write post data DataOutputStream out = new DataOutputStream(conn.getOutputStream()); out.writeBytes(xml); out.flush(); out.close(); BufferedReader aiResult = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; StringBuffer responseMessage = new StringBuffer(); while ((line = aiResult.readLine()) != null) { responseMessage.append(line); } System.out.println(responseMessage); //urlc.s("POST"); // urlc.setRequestProperty("SOAPAction", SOAP_ACTION); urlc.connect(); } catch (MalformedURLException ex) { Logger.getLogger(AirtelCharging.class .getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(AirtelCharging.class .getName()).log(Level.SEVERE, null, ex); } return result.toString(); // try { // // String url = "https://selfsolve.apple.com/wcResults.do"; // HttpClient client = new DefaultHttpClient(); // HttpPost post = new HttpPost("https://41.223.58.133:8443/ChargingServiceFlowWeb/sca/ChargingExport1"); // // add header // post.setHeader("User-Agent", USER_AGENT); // post.setHeader("Content-type:", " text/xml"); // post.setHeader("charset", "utf-8"); // post.setHeader("Accept:", ",text/xml"); // post.setHeader("Cache-Control:", " no-cache"); // post.setHeader("Pragma:", " no-cache"); // post.setHeader("SOAPAction:", "run"); // post.setHeader("Content-length:", "xml"); // //// String encoding = new Base64().encode((networkproperties.getProperty("air_charge_n") + ":" //// + networkproperties.getProperty("air_charge_p")).getBytes()); // String encoding = new Base64().encode( ("roamtech_KE:roamtech _KE!ibm123").getBytes()); // // post.setHeader("Authorization", "Basic " + encoding); // // // post.setHeader("Authorization: Basic ", "base64_encode(credentials)"); // List<NameValuePair> urlParameters = new ArrayList<NameValuePair>(); // // urlParameters.add(new BasicNameValuePair("xml", xml)); // // System.out.println("\n============================ : " + url); // //// urlParameters.add(new BasicNameValuePair("srcCode", "")); //// urlParameters.add(new BasicNameValuePair("phone", "")); //// urlParameters.add(new BasicNameValuePair("contentId", "")); //// urlParameters.add(new BasicNameValuePair("itemName", "")); //// urlParameters.add(new BasicNameValuePair("contentDescription", "")); //// urlParameters.add(new BasicNameValuePair("actualPrice", "")); //// urlParameters.add(new BasicNameValuePair("contentMediaType", "")); //// urlParameters.add(new BasicNameValuePair("contentUrl", "")); // post.setEntity(new UrlEncodedFormEntity(urlParameters)); // // HttpResponse response = client.execute(post); // Launcher.LOG.info("\nSending 'POST' request to URL : " + url); // Launcher.LOG.info("Post parameters : " + post.getEntity()); // Launcher.LOG.info("Response Code : " // + response.getStatusLine().getStatusCode()); // // BufferedReader rd = new BufferedReader( // new InputStreamReader(response.getEntity().getContent())); // // String line = ""; // while ((line = rd.readLine()) != null) { // result.append(line); // } // // System.out.println(result.toString()); // } catch (UnsupportedEncodingException ex) { // Launcher.LOG.info(ex.getMessage()); // } catch (IOException ex) { // Launcher.LOG.info(ex.getMessage()); // } }
From source file:org.wso2.carbon.mediation.registry.ESBRegistry.java
public OMNode lookup(String key) { if (log.isDebugEnabled()) { log.info("==> Repository fetch of resource with key : " + key); }/* w w w. ja v a 2 s. co m*/ URLConnection urlc; URL url = null; try { url = new URL(getRoot() + key); } catch (MalformedURLException e) { handleException("Invalid path '" + getRoot() + key + "' for URL", e); } if ("file".equals(url.getProtocol())) { try { url.openStream(); } catch (IOException ignored) { if (!localRegistry.endsWith(URL_SEPARATOR)) { localRegistry = localRegistry + URL_SEPARATOR; } try { url = new URL(url.getProtocol() + ":" + localRegistry + key); } catch (MalformedURLException e) { handleException("Invalid path '" + url.getProtocol() + ":" + localRegistry + key + "' for URL", e); } try { url.openStream(); } catch (IOException e) { return null; } } } try { urlc = url.openConnection(); urlc.connect(); } catch (IOException e) { return null; } InputStream input = null; try { input = urlc.getInputStream(); } catch (IOException e) { handleException("Error when getting a stream from the URL", e); } if (input == null) { return null; } BufferedInputStream inputStream = new BufferedInputStream(input); OMNode result = null; try { XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(inputStream); StAXOMBuilder builder = new StAXOMBuilder(parser); result = builder.getDocumentElement(); } catch (OMException ignored) { if (log.isDebugEnabled()) { log.debug("The resource at the provided URL isn't " + "well-formed XML,So,takes it as a text"); } try { inputStream.close(); } catch (IOException e) { log.error("Error in closing the input stream. ", e); } result = SynapseConfigUtils.readNonXML(url); } catch (XMLStreamException ignored) { if (log.isDebugEnabled()) { log.debug("The resource at the provided URL isn't " + "well-formed XML,So,takes it as a text"); } try { inputStream.close(); } catch (IOException e) { log.error("Error in closing the input stream. ", e); } result = SynapseConfigUtils.readNonXML(url); } finally { try { result.detach(); OMDocumentImpl parent = new OMDocumentImpl(OMAbstractFactory.getOMFactory()); parent.addChild(result); inputStream.close(); } catch (IOException e) { log.error("Error in closing the input stream.", e); } } return result; }
From source file:hudson.FilePath.java
/** * Given a tgz/zip file, extracts it to the given target directory, if necessary. * * <p>/*from w w w .jav a2 s .c om*/ * This method is a convenience method designed for installing a binary package to a location * that supports upgrade and downgrade. Specifically, * * <ul> * <li>If the target directory doesn't exist {@linkplain #mkdirs() it'll be created}. * <li>The timestamp of the .tgz file is left in the installation directory upon extraction. * <li>If the timestamp left in the directory doesn't match with the timestamp of the current archive file, * the directory contents will be discarded and the archive file will be re-extracted. * <li>If the connection is refused but the target directory already exists, it is left alone. * </ul> * * @param archive * The resource that represents the tgz/zip file. This URL must support the "Last-Modified" header. * (Most common usage is to get this from {@link ClassLoader#getResource(String)}) * @param listener * If non-null, a message will be printed to this listener once this method decides to * extract an archive. * @return * true if the archive was extracted. false if the extraction was skipped because the target directory * was considered up to date. * @since 1.299 */ public boolean installIfNecessaryFrom(URL archive, TaskListener listener, String message) throws IOException, InterruptedException { URLConnection con; try { con = archive.openConnection(); con.connect(); } catch (IOException x) { if (this.exists()) { // Cannot connect now, so assume whatever was last unpacked is still OK. if (listener != null) { listener.getLogger() .println("Skipping installation of " + archive + " to " + remote + ": " + x); } return false; } else { throw x; } } long sourceTimestamp = con.getLastModified(); FilePath timestamp = this.child(".timestamp"); if (this.exists()) { if (timestamp.exists() && sourceTimestamp == timestamp.lastModified()) return false; // already up to date this.deleteContents(); } if (listener != null) listener.getLogger().println(message); CountingInputStream cis = new CountingInputStream(con.getInputStream()); try { if (archive.toExternalForm().endsWith(".zip")) unzipFrom(cis); else untarFrom(cis, GZIP); } catch (IOException e) { throw new IOException2(String.format("Failed to unpack %s (%d bytes read of total %d)", archive, cis.getByteCount(), con.getContentLength()), e); } timestamp.touch(sourceTimestamp); return true; }