List of usage examples for java.net URLConnection getURL
public URL getURL()
From source file:org.wise.portal.presentation.web.controllers.admin.ImportProjectController.java
@RequestMapping(value = "/admin/project/importFromHub", method = RequestMethod.POST) protected String importFromHub( @RequestParam(value = "importableProjectId", required = true) String importableProjectId, ModelMap modelMap) throws Exception { // URL to get the importableProject zip file String getImportableProjectURL = getWISEProjectsURL + "?id=" + importableProjectId; try {//from w w w.java 2 s . com URL url = new URL(getImportableProjectURL); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); URL downloadFileUrl = conn.getURL(); String downloadFileUrlString = downloadFileUrl.toString(); String zipFilename = downloadFileUrlString.substring(downloadFileUrlString.lastIndexOf("/") + 1); byte[] fileBytes = IOUtils.toByteArray(in); String msg = "Import project complete!"; Project project = importProject(zipFilename, fileBytes); if (project == null) { System.err.println("Error occured during project import."); msg = "Error occured during project import. Check the log for more information."; } modelMap.put("msg", msg); modelMap.put("newProject", project); return "admin/project/import"; } catch (IOException e) { System.err.println("Error occured during project import."); e.printStackTrace(); } return "admin/project/import"; }
From source file:org.wise.portal.service.wiseup.impl.WiseUpServiceImpl.java
/** * @throws Exception /* w ww . j a v a 2s . c o m*/ * @see org.wise.portal.service.wiseup.WiseUpService#importExternalProject(net.sf.sail.webapp.domain.User, java.lang.String, java.lang.String) */ @Override public void importExternalProject(User newProjectOwner, String externalWiseInstanceId, String externalWiseProjectId) throws Exception { String exportProjectPath = wiseUpHubUrl + "/projectLibrary/exportProject.php" + "?wiseInstanceId=" + externalWiseInstanceId + "&wiseProjectId=" + externalWiseProjectId; // upload the zipfile to curriculum_base_dir String curriculumBaseDir = wiseProperties.getProperty("curriculum_base_dir"); File uploadDir = new File(curriculumBaseDir); if (!uploadDir.exists()) { throw new Exception("curriculum upload directory does not exist."); } // save the downloaded zip file temporarily in the curriculum folder. String sep = System.getProperty("file.separator"); long timeInMillis = Calendar.getInstance().getTimeInMillis(); String filename = ""; String newFilename = ""; String newFileFullPath = ""; File downloadedFile = null; try { URL url = new URL(exportProjectPath); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); URL downloadFileUrl = conn.getURL(); String downloadFileUrlString = downloadFileUrl.toString(); filename = downloadFileUrlString.substring(downloadFileUrlString.lastIndexOf("/") + 1, downloadFileUrlString.indexOf(".zip")); newFilename = filename; if (new File(curriculumBaseDir + sep + filename).exists()) { // if this directory already exists, add a date time in milliseconds to the filename to make it unique newFilename = filename + "-" + timeInMillis; } newFileFullPath = curriculumBaseDir + sep + newFilename + ".zip"; downloadedFile = new File(newFileFullPath); FileOutputStream out = new FileOutputStream(downloadedFile); byte[] b = new byte[1024]; int count; while ((count = in.read(b)) >= 0) { out.write(b, 0, count); } out.flush(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } // make a new folder where the contents of the zip should go String newFileFullDir = curriculumBaseDir + sep + newFilename; File newFileFullDirFile = new File(newFileFullDir); newFileFullDirFile.mkdir(); // unzip the zip file try { ZipFile zipFile = new ZipFile(newFileFullPath); Enumeration entries = zipFile.entries(); int i = 0; // index used later to check for first folder in the zip file while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.getName().startsWith("__MACOSX")) { // if this entry starts with __MACOSX, this zip file was created by a user using mac's "compress" feature. // ignore it. continue; } if (entry.isDirectory()) { // first check to see if the user has changed the zip file name and therefore the zipfile name // is no longer the same as the name of the first folder in the top-level of the zip file. // if this is the case, import will fail, so throw an error. if (i == 0) { if (!entry.getName().startsWith(filename)) { throw new Exception( "Zip file name does not match folder name. Do not change zip filename"); } i++; } // Assume directories are stored parents first then children. System.out.println("Extracting directory: " + entry.getName()); // This is not robust, just for demonstration purposes. (new File(entry.getName().replace(filename, newFileFullDir))).mkdir(); continue; } System.out.println("Extracting file: " + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(entry.getName().replaceFirst(filename, newFileFullDir)))); } zipFile.close(); } catch (IOException ioe) { System.err.println("Unhandled exception during project import. Project was not properly imported."); ioe.printStackTrace(); throw ioe; } // remove the temp zip file downloadedFile.delete(); // now create a project in the db with the new path String path = sep + newFilename + sep + "wise4.project.json"; String name = "hiroki's project 4.zip"; Set<User> owners = new HashSet<User>(); owners.add(newProjectOwner); CreateUrlModuleParameters cParams = new CreateUrlModuleParameters(); cParams.setUrl(path); Curnit curnit = curnitService.createCurnit(cParams); ProjectParameters pParams = new ProjectParameters(); pParams.setCurnitId(curnit.getId()); pParams.setOwners(owners); pParams.setProjectname(name); pParams.setProjectType(ProjectType.LD); ProjectMetadata metadata = null; // see if a file called wise4.project-meta.json exists. if yes, try parsing it. try { String projectMetadataFilePath = newFileFullDir + sep + "wise4.project-meta.json"; String projectMetadataStr = FileUtils.readFileToString(new File(projectMetadataFilePath)); JSONObject metadataJSONObj = new JSONObject(projectMetadataStr); metadata = new ProjectMetadataImpl(); metadata.populateFromJSON(metadataJSONObj); } catch (Exception e) { // if there is any error during the parsing of the metadata, set the metadata to null metadata = null; } // If metadata is null at this point, either wise4.project-meta.json was not // found in the zip file, or there was an error parsing. // Set a new fresh metadata object if (metadata == null) { metadata = new ProjectMetadataImpl(); metadata.setTitle(name); } pParams.setMetadata(metadata); Project project = projectService.createProject(pParams); }
From source file:org.telscenter.sail.webapp.service.wiseup.impl.WiseUpServiceImpl.java
/** * @throws Exception // ww w . j a va 2 s .c o m * @see org.telscenter.sail.webapp.service.wiseup.WiseUpService#importExternalProject(net.sf.sail.webapp.domain.User, java.lang.String, java.lang.String) */ @Override public void importExternalProject(User newProjectOwner, String externalWiseInstanceId, String externalWiseProjectId) throws Exception { String exportProjectPath = wiseUpHubUrl + "/projectLibrary/exportProject.php" + "?wiseInstanceId=" + externalWiseInstanceId + "&wiseProjectId=" + externalWiseProjectId; // upload the zipfile to curriculum_base_dir String curriculumBaseDir = portalProperties.getProperty("curriculum_base_dir"); File uploadDir = new File(curriculumBaseDir); if (!uploadDir.exists()) { throw new Exception("curriculum upload directory does not exist."); } // save the downloaded zip file temporarily in the curriculum folder. String sep = System.getProperty("file.separator"); long timeInMillis = Calendar.getInstance().getTimeInMillis(); String filename = ""; String newFilename = ""; String newFileFullPath = ""; File downloadedFile = null; try { URL url = new URL(exportProjectPath); URLConnection conn = url.openConnection(); InputStream in = conn.getInputStream(); URL downloadFileUrl = conn.getURL(); String downloadFileUrlString = downloadFileUrl.toString(); filename = downloadFileUrlString.substring(downloadFileUrlString.lastIndexOf("/") + 1, downloadFileUrlString.indexOf(".zip")); newFilename = filename; if (new File(curriculumBaseDir + sep + filename).exists()) { // if this directory already exists, add a date time in milliseconds to the filename to make it unique newFilename = filename + "-" + timeInMillis; } newFileFullPath = curriculumBaseDir + sep + newFilename + ".zip"; downloadedFile = new File(newFileFullPath); FileOutputStream out = new FileOutputStream(downloadedFile); byte[] b = new byte[1024]; int count; while ((count = in.read(b)) >= 0) { out.write(b, 0, count); } out.flush(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } // make a new folder where the contents of the zip should go String newFileFullDir = curriculumBaseDir + sep + newFilename; File newFileFullDirFile = new File(newFileFullDir); newFileFullDirFile.mkdir(); // unzip the zip file try { ZipFile zipFile = new ZipFile(newFileFullPath); Enumeration entries = zipFile.entries(); int i = 0; // index used later to check for first folder in the zip file while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.getName().startsWith("__MACOSX")) { // if this entry starts with __MACOSX, this zip file was created by a user using mac's "compress" feature. // ignore it. continue; } if (entry.isDirectory()) { // first check to see if the user has changed the zip file name and therefore the zipfile name // is no longer the same as the name of the first folder in the top-level of the zip file. // if this is the case, import will fail, so throw an error. if (i == 0) { if (!entry.getName().startsWith(filename)) { throw new Exception( "Zip file name does not match folder name. Do not change zip filename"); } i++; } // Assume directories are stored parents first then children. System.out.println("Extracting directory: " + entry.getName()); // This is not robust, just for demonstration purposes. (new File(entry.getName().replace(filename, newFileFullDir))).mkdir(); continue; } System.out.println("Extracting file: " + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream( new FileOutputStream(entry.getName().replaceFirst(filename, newFileFullDir)))); } zipFile.close(); } catch (IOException ioe) { System.err.println("Unhandled exception during project import. Project was not properly imported."); ioe.printStackTrace(); throw ioe; } // remove the temp zip file downloadedFile.delete(); // now create a project in the db with the new path String path = sep + newFilename + sep + "wise4.project.json"; String name = "hiroki's project 4.zip"; Set<User> owners = new HashSet<User>(); owners.add(newProjectOwner); CreateUrlModuleParameters cParams = new CreateUrlModuleParameters(); cParams.setUrl(path); Curnit curnit = curnitService.createCurnit(cParams); ProjectParameters pParams = new ProjectParameters(); pParams.setCurnitId(curnit.getId()); pParams.setOwners(owners); pParams.setProjectname(name); pParams.setProjectType(ProjectType.LD); ProjectMetadata metadata = null; // see if a file called wise4.project-meta.json exists. if yes, try parsing it. try { String projectMetadataFilePath = newFileFullDir + sep + "wise4.project-meta.json"; String projectMetadataStr = FileUtils.readFileToString(new File(projectMetadataFilePath)); JSONObject metadataJSONObj = new JSONObject(projectMetadataStr); metadata = new ProjectMetadataImpl(); metadata.populateFromJSON(metadataJSONObj); } catch (Exception e) { // if there is any error during the parsing of the metadata, set the metadata to null metadata = null; } // If metadata is null at this point, either wise4.project-meta.json was not // found in the zip file, or there was an error parsing. // Set a new fresh metadata object if (metadata == null) { metadata = new ProjectMetadataImpl(); metadata.setTitle(name); } pParams.setMetadata(metadata); Project project = projectService.createProject(pParams); }
From source file:io.s4.latin.adapter.TwitterFeedListener.java
public void connectAndRead() throws Exception { URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setConnectTimeout(10000); connection.setReadTimeout(10000);// ww w . j a va 2 s . c o m String userPassword = userid + ":" + password; System.out.println("connect to " + connection.getURL().toString() + " ..."); String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword))); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setRequestProperty("Accept-Charset", "utf-8,ISO-8859-1"); connection.connect(); System.out.println("Connect OK!"); System.out.println("Reading TwitterFeed ...."); Long startTime = new Date().getTime(); InputStream is = connection.getInputStream(); Charset utf8 = Charset.forName("UTF-8"); InputStreamReader isr = new InputStreamReader(is, utf8); BufferedReader br = new BufferedReader(isr); String inputLine = null; while ((inputLine = br.readLine()) != null) { if (inputLine.trim().length() == 0) { blankCount++; continue; } messageCount++; messageQueue.add(inputLine); if (messageCount % 500 == 0) { Long currentTime = new Date().getTime(); System.out.println("Lines processed: " + messageCount + "\t ( " + blankCount + " empty lines ) in " + (currentTime - startTime) / 1000 + " seconds. Reading " + messageCount / ((currentTime - startTime) / 1000) + " rows/second"); } } }
From source file:info.ajaxplorer.client.http.AjxpAPI.java
public void enrichConnexionWithCookies(URLConnection connexion) { try {/*from www .j a v a2 s .co m*/ URI uri = new URI(connexion.getURL().toString()); List<Cookie> cookies = AjxpHttpClient.getCookies(uri); String cookieString = ""; for (int i = 0; i < cookies.size(); i++) { String cs = ""; if (i > 0) { cs += "; "; } cs += cookies.get(i).getName(); cs += "="; cs += cookies.get(i).getValue(); cookieString += cs; } connexion.setRequestProperty("Cookie", cookieString); } catch (Exception e) { } }
From source file:com.moviejukebox.themoviedb.tools.WebBrowser.java
private static void readHeader(URLConnection cnx) { // read new cookies and update our cookies for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) { if ("Set-Cookie".equals(header.getKey())) { for (String cookieHeader : header.getValue()) { String[] cookieElements = cookieHeader.split(" *; *"); if (cookieElements.length >= 1) { String[] firstElem = cookieElements[0].split(" *= *"); String cookieName = firstElem[0]; String cookieValue = firstElem.length > 1 ? firstElem[1] : null; String cookieDomain = null; // find cookie domain for (int i = 1; i < cookieElements.length; i++) { String[] cookieElement = cookieElements[i].split(" *= *"); if ("domain".equals(cookieElement[0])) { cookieDomain = cookieElement.length > 1 ? cookieElement[1] : null; break; }//ww w .j a va2s . com } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } Map<String, String> domainCookies = cookies.get(cookieDomain); if (domainCookies == null) { domainCookies = new HashMap<String, String>(); cookies.put(cookieDomain, domainCookies); } // add or replace cookie domainCookies.put(cookieName, cookieValue); } } } } }
From source file:com.omertron.themoviedbapi.tools.WebBrowser.java
private static void readHeader(URLConnection cnx) { // read new cookies and update our cookies for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) { if ("Set-Cookie".equals(header.getKey())) { for (String cookieHeader : header.getValue()) { String[] cookieElements = cookieHeader.split(" *; *"); if (cookieElements.length >= 1) { String[] firstElem = cookieElements[0].split(" *= *"); String cookieName = firstElem[0]; String cookieValue = firstElem.length > 1 ? firstElem[1] : null; String cookieDomain = null; // find cookie domain for (int i = 1; i < cookieElements.length; i++) { String[] cookieElement = cookieElements[i].split(" *= *"); if ("domain".equals(cookieElement[0])) { cookieDomain = cookieElement.length > 1 ? cookieElement[1] : null; break; }//from w w w . j a v a2 s. c o m } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } Map<String, String> domainCookies = COOKIES.get(cookieDomain); if (domainCookies == null) { domainCookies = new HashMap<String, String>(); COOKIES.put(cookieDomain, domainCookies); } // add or replace cookie domainCookies.put(cookieName, cookieValue); } } } } }
From source file:com.omertron.thetvdbapi.tools.WebBrowser.java
/** * Read the header information into the cookies * * @param cnx// ww w .j a va 2s. c om */ private static void readHeader(URLConnection cnx) { // read new cookies and update our cookies for (Map.Entry<String, List<String>> header : cnx.getHeaderFields().entrySet()) { if ("Set-Cookie".equals(header.getKey())) { for (String cookieHeader : header.getValue()) { String[] cookieElements = cookieHeader.split(" *; *"); if (cookieElements.length >= 1) { String[] firstElem = cookieElements[0].split(" *= *"); String cookieName = firstElem[0]; String cookieValue = firstElem.length > 1 ? firstElem[1] : null; String cookieDomain = null; // find cookie domain for (int i = 1; i < cookieElements.length; i++) { String[] cookieElement = cookieElements[i].split(" *= *"); if ("domain".equals(cookieElement[0])) { cookieDomain = cookieElement.length > 1 ? cookieElement[1] : null; break; } } if (cookieDomain == null) { // if domain isn't set take current host cookieDomain = cnx.getURL().getHost(); } Map<String, String> domainCookies = cookies.get(cookieDomain); if (domainCookies == null) { domainCookies = new HashMap<String, String>(); cookies.put(cookieDomain, domainCookies); } // add or replace cookie domainCookies.put(cookieName, cookieValue); } } } } }
From source file:org.eclipse.ecr.web.framework.io.URLConnectionMessageBodyWriter.java
@Override public void writeTo(URLConnection conn, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try {//from www . j a v a2s . co m java.io.InputStream in = conn.getInputStream(); try { FileUtils.copy(in, entityStream); } finally { try { entityStream.flush(); } finally { in.close(); } } } catch (Throwable e) { log.error("Failed to get resource: " + conn.getURL(), e); throw new IOException("Failed to get resource: " + conn.getURL(), e); } }
From source file:de.indiplex.javapt.JavAPT.java
public void updateSources(boolean downloadSs) throws URISyntaxException, IOException, SQLException { Statement stat = db.getStat(); stat.executeUpdate("DELETE FROM packages;"); debs = new ArrayList<DEB>(); int last = 0; for (String u : sourcesURLs) { String pre = new URL(u).getHost(); File out = new File(dTemp, pre + ".source"); if (downloadSs) { URLConnection con = new URL(u.toString() + "Packages.bz2").openConnection(); File tmpOut = new File(dTemp, pre + ".source.bz2"); System.out.println("Downloading " + con.getURL() + "..."); download(con, tmpOut);/*ww w . ja v a 2s . c om*/ System.out.println("Decompressing..."); CountingFileInputStream fis = new CountingFileInputStream(tmpOut); BZip2CompressorInputStream in = new BZip2CompressorInputStream(fis); Finish = tmpOut.length(); Util.checkFiles(out); OutputStream fout = new FileOutputStream(out); int i = in.read(); while (i != -1) { State = fis.getCount(); fout.write(i); i = in.read(); } fout.close(); in.close(); } BufferedReader br = new BufferedReader(new FileReader(out)); DEB deb = null; ArrayList<String> lines = new ArrayList<String>(); while (br.ready()) { String line = br.readLine(); lines.add(line); } Finish = lines.size(); State = 0; System.out.println("Parsing..."); for (String line : lines) { State++; if (line.startsWith("Package")) { if (deb != null) { debs.add(deb); } deb = new DEB(p(line)); } if (line.startsWith("Depends")) { if (deb == null) { continue; } deb.depends = p(line).trim(); deb.depends = deb.depends.replaceAll("\\({1}[^\\(]*\\)", ""); } if (line.startsWith("Provides")) { if (deb == null) { continue; } deb.provides = p(line); } if (line.startsWith("Filename")) { if (deb == null) { continue; } deb.filename = u + p(line); if (u.equals("http://apt.saurik.com/dists/ios/675.00/main/binary-iphoneos-arm/")) { deb.filename = "http://apt.saurik.com/" + p(line); } if (u.contains("apt.thebigboss")) { deb.filename = deb.filename.replace("dists/stable/main/binary-iphoneos-arm/", ""); } deb.filename = deb.filename.replace("http://", ""); deb.filename = deb.filename.replace("//", "/"); deb.filename = "http://" + deb.filename; } } System.out.println("Updated " + u + "! " + (debs.size() - last) + " packages found"); last = debs.size(); } PreparedStatement pstat = db .getPreparedStatement("INSERT INTO packages (name, depends, provides, filename) VALUES (?,?,?,?);"); for (DEB deb : debs) { pstat.setString(1, deb.packagename); pstat.setString(2, deb.depends); pstat.setString(3, deb.provides); pstat.setString(4, deb.filename); pstat.addBatch(); } db.closePS(); if (isGUI()) { mf.fillPackages(debs); } hashDEB(); }