List of usage examples for java.net URL getPath
public String getPath()
From source file:com.nanocrawler.robotstxt.RobotstxtServer.java
public boolean allows(WebURL webURL) { try {/*w ww . j av a 2 s . c o m*/ URL url = new URL(webURL.getURL()); String host = getHost(url); String path = url.getPath(); HostDirectives directives = host2directivesCache.get(host); if (directives != null && directives.needsRefetch()) { synchronized (host2directivesCache) { host2directivesCache.remove(host); directives = null; } } if (directives == null) { directives = fetchDirectives(url); } return directives.allows(path); } catch (MalformedURLException e) { e.printStackTrace(); } return true; }
From source file:at.uni_salzburg.cs.ckgroup.cscpp.mapper.registry.RegistryPersistenceTestCase.java
@Test public void test() throws IOException, ParseException { URL storage = RegistryPersistenceTestCase.class.getResource("storage01.dat"); File storagePath = new File(storage.getPath()); Map<String, IRegistrationData> registrationData = new HashMap<String, IRegistrationData>(); RegistryPersistence.loadRegistry(storagePath, registrationData); IRegistrationData d1 = registrationData.get("http://localhost:8080/engine"); Assert.assertNotNull(d1);//ww w .j a va 2s. c o m String engineUri = d1.getEngineUrl(); Assert.assertEquals("http://localhost:8080/engine", engineUri); String pilotUri = d1.getPilotUrl(); Assert.assertEquals("http://localhost:8080/pilot", pilotUri); Set<String> sensors = d1.getSensors(); Assert.assertEquals(7, sensors.size()); Assert.assertTrue(sensors.contains("sonar")); Assert.assertTrue(sensors.contains("position")); Assert.assertTrue(sensors.contains("random")); Assert.assertTrue(sensors.contains("photo")); Assert.assertTrue(sensors.contains("video")); Assert.assertTrue(sensors.contains("airPressure")); Assert.assertTrue(sensors.contains("temperature")); List<IWayPoint> wayPoints = d1.getWaypoints(); Assert.assertEquals(17, wayPoints.size()); Assert.assertTrue(wayPoints.get(0) instanceof WayPoint); WayPoint wayPoint = (WayPoint) wayPoints.get(0); System.out.println("test() " + wayPoint.toJSONString()); Assert.assertEquals( "{\"precision\":1.0,\"altitude\":1.0,\"velocity\":1.0,\"longitude\":13.04092571,\"latitude\":47.82204197}", wayPoint.toJSONString()); Assert.assertEquals("(47.82204197, 13.04092571, 1.000) precision 1 velocity 1.0", wayPoints.get(0).toString()); File tmpFile = File.createTempFile("mapper", ".dat"); RegistryPersistence.storeRegistry(tmpFile, registrationData); String expected = FileUtils.loadFileAsString(storagePath); Assert.assertEquals(1860, expected.length()); String actual = FileUtils.loadFileAsString(tmpFile); Assert.assertEquals(1860, actual.length()); Assert.assertEquals(expected, actual); tmpFile.delete(); }
From source file:com.gargoylesoftware.htmlunit.util.UrlUtils.java
/** * <p>Encodes illegal characters in the specified URL's path, query string and anchor according to the URL * encoding rules observed in real browsers.</p> * * <p>For example, this method changes <tt>"http://first/?a=b c"</tt> to <tt>"http://first/?a=b%20c"</tt>.</p> * * @param url the URL to encode//from w w w. j a v a 2s. c o m * @param minimalQueryEncoding whether or not to perform minimal query encoding, like IE does * @param charset the charset * @return the encoded URL */ public static URL encodeUrl(final URL url, final boolean minimalQueryEncoding, final String charset) { if (!isNormalUrlProtocol(URL_CREATOR.getProtocol(url))) { return url; // javascript:, about:, data: and anything not supported like foo: } try { String path = url.getPath(); if (path != null) { path = encode(path, PATH_ALLOWED_CHARS, "UTF-8"); } String query = url.getQuery(); if (query != null) { if (minimalQueryEncoding) { query = org.apache.commons.lang3.StringUtils.replace(query, " ", "%20"); } else { query = encode(query, QUERY_ALLOWED_CHARS, charset); } } String anchor = url.getRef(); if (anchor != null) { anchor = encode(anchor, ANCHOR_ALLOWED_CHARS, "UTF-8"); } return createNewUrl(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), path, anchor, query); } catch (final MalformedURLException e) { // Impossible... I think. throw new RuntimeException(e); } }
From source file:fr.gael.dhus.server.http.webapp.stub.controller.AppConfigController.java
@RequestMapping(value = "/configuration", method = RequestMethod.GET) public ResponseEntity<?> getAppConfig() throws JSONException, FileNotFoundException, IOException { URL configFile = ClassLoader.getSystemResource("../etc/conf/appconfig.json"); if (configFile != null) { logger.debug("Loading configuration file " + configFile.getPath()); try {/* ww w .j av a 2 s. c o m*/ File file = new File(configFile.getPath()); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); String line = ""; StringBuffer sb = new StringBuffer(); while ((line = bufferedReader.readLine()) != null) { sb.append(line); } bufferedReader.close(); JSONObject appconfig = new JSONObject(sb.toString()); return new ResponseEntity<>(appconfig.toString(), HttpStatus.OK); } catch (IOException e) { logger.error(" Cannot load appconfig file content"); e.printStackTrace(); return new ResponseEntity<>("{\"code\":\"not_found\"}", HttpStatus.NOT_FOUND); } } else { logger.error(" Cannot get appconfig file "); return new ResponseEntity<>("{\"code\":\"not_found\"}", HttpStatus.NOT_FOUND); } }
From source file:it.marcoberri.mbfasturl.helper.ConfigurationHelper.java
private ConfigurationHelper() { try {//from w ww . j a v a 2 s . co m final URL main = getClass().getProtectionDomain().getCodeSource().getLocation(); final String path = URLDecoder.decode(main.getPath(), "utf-8"); final String webInfFolderPosition = new File(path).getPath(); final String webInfFolder = webInfFolderPosition.substring(0, webInfFolderPosition.indexOf("classes")); prop = new Properties(); prop.load(FileUtils.openInputStream(new File(webInfFolder + File.separator + "config.properties"))); final JSONParser parser = new JSONParser(); final Object obj = parser.parse(new FileReader(webInfFolder + File.separator + "cron.json")); final JSONObject jsonObject = (JSONObject) obj; ConfigurationHelper.cron = (JSONArray) jsonObject.get("cron"); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (ParseException ex) { throw new RuntimeException(ex.getMessage(), ex); } }
From source file:com.strandls.alchemy.rest.client.stubgenerator.RestProxyGeneratorTest.java
/** * Run the task.// w w w . java 2s . c om * * @param outputDir * the output directory. * @param destinationPackage * the destination package name. * @throws IOException */ private void runProxyGenerateTask(final File outputDir, final String destinationPackage) throws IOException { final BuildLogger logger = new DefaultLogger(); logger.setMessageOutputLevel(Project.MSG_INFO); logger.setOutputPrintStream(System.out); logger.setErrorPrintStream(System.out); logger.setEmacsMode(true); final ProjectHelper ph = ProjectHelper.getProjectHelper(); final Project p = new Project(); p.addBuildListener(logger); p.init(); p.addReference("ant.projectHelper", ph); final URL[] urls = ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs(); final StringBuffer classPath = new StringBuffer(); for (final URL url : urls) { classPath.append(new File(url.getPath())); classPath.append(System.getProperty("path.separator")); } classPath.append("src/test/resources/test-webservices.jar"); // set properties p.setUserProperty("client.src.dir", outputDir.getAbsolutePath()); p.setUserProperty("task.classpath", classPath.toString()); if (destinationPackage != null) { p.setUserProperty("destinationPackage", destinationPackage); } ph.parse(p, new File("src/test/resources/restProxyGenBuild.xml")); p.executeTarget("restProxyGen"); // ensure all files are generated final Set<String> expectedFiles = new HashSet<>( FileUtils.readLines(new File("src/test/resources/test-weservices-files.txt"))); final File packageDir = new File(outputDir, destinationPackage.replaceAll("\\.", File.separator)); final Set<String> actualFiles = new HashSet<String>(); for (final File file : packageDir.listFiles()) { if (file.isFile()) { actualFiles.add(file.getName()); } } assertEquals(expectedFiles, actualFiles); }
From source file:hydrograph.ui.expression.editor.buttons.ValidateExpressionToolButton.java
public static Object[] getBuildPathForMethodInvocation() throws JavaModelException, MalformedURLException { String transfromJarPath = null; Object[] returnObj = new Object[3]; IJavaProject iJavaProject = JavaCore .create(BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject()); List<URL> urlList = new ArrayList<>(); Properties properties = ConfigFileReader.INSTANCE.getCommonConfigurations(); for (IPackageFragmentRoot iPackageFragmentRoot : iJavaProject.getAllPackageFragmentRoots()) { if (!iPackageFragmentRoot.isExternal() || StringUtils.contains(iPackageFragmentRoot.getElementName(), properties.getProperty(Constants.KEY_TRANSFORMATION_JAR)) || StringUtils.contains(iPackageFragmentRoot.getElementName(), Constants.ANTLR_JAR_FILE_NAME) || StringUtils.contains(iPackageFragmentRoot.getElementName(), Constants.BEAN_SHELLJAR_FILE_NAME) || StringUtils.contains(iPackageFragmentRoot.getElementName(), Constants.SL4JLOG) || StringUtils.contains(iPackageFragmentRoot.getElementName(), properties.getProperty(Constants.KEY_EXPRESSION_JAR))) { URL url = null; if (!iPackageFragmentRoot.isExternal()) { url = BuildExpressionEditorDataSturcture.INSTANCE.getCurrentProject() .getFile(iPackageFragmentRoot.getPath().removeFirstSegments(1)).getLocation().toFile() .toURI().toURL(); urlList.add(url);/*w ww . jav a 2s . co m*/ } else { url = iPackageFragmentRoot.getPath().toFile().toURI().toURL(); urlList.add(url); } if (!iPackageFragmentRoot.isExternal() || StringUtils.contains(iPackageFragmentRoot.getElementName(), properties.getProperty(Constants.KEY_TRANSFORMATION_JAR))) { if (transfromJarPath == null) { if (OSValidator.isMac() || OSValidator.isUnix()) transfromJarPath = url.getPath() + Constants.COLON; else transfromJarPath = url.getPath() + Constants.SEMICOLON; } else { if (OSValidator.isMac() || OSValidator.isUnix()) transfromJarPath = transfromJarPath + url.getPath() + Constants.COLON; else transfromJarPath = transfromJarPath + url.getPath() + Constants.SEMICOLON; } } } } returnObj[0] = urlList; returnObj[1] = transfromJarPath; returnObj[2] = getPropertyFilePath(iJavaProject); iJavaProject.close(); return returnObj; }
From source file:de.comlineag.snc.webcrawler.robotstxt.RobotstxtServer.java
public boolean allows(WebURL webURL) { if (!config.isEnabled()) { return true; }/* w w w . j a va2 s .co m*/ try { URL url = new URL(webURL.getURL()); String host = getHost(url); String path = url.getPath(); HostDirectives directives = host2directivesCache.get(host); if (directives != null && directives.needsRefetch()) { synchronized (host2directivesCache) { host2directivesCache.remove(host); directives = null; } } if (directives == null) { directives = fetchDirectives(url); } return directives.allows(path); } catch (MalformedURLException e) { e.printStackTrace(); } return true; }
From source file:ua.pp.msk.cliqr.GetProcessorImpl.java
@Override public String getResponse(URL url) throws ResponseException { String apiPath = url.getPath(); return getResponse(true, apiPath); }
From source file:com.thinkberg.webdav.WebdavHandler.java
/** * Get the destination object or collection. The destination header contains * a URL to the destination which is returned as a file object. * * @param request the servlet request//from w w w . ja v a 2s.c om * @return the file object of the destination * @throws FileSystemException if the file system cannot create a file object * @throws MalformedURLException if the url is misformatted */ FileObject getDestination(HttpServletRequest request) throws FileSystemException, MalformedURLException { String targetUrlStr = request.getHeader("Destination"); FileObject targetObject = null; if (null != targetUrlStr) { URL target = new URL(targetUrlStr); targetObject = VFSBackend.resolveFile(target.getPath()); LOG.debug(String.format("request header: Destination: %s", targetObject.getName().getPath())); } return targetObject; }