List of usage examples for java.net URL getPath
public String getPath()
From source file:gov.loc.ndmso.proxyfilter.RequestProxy.java
private static HttpMethod setupProxyRequest(final HttpServletRequest hsRequest, final URL targetUrl) throws IOException { final String methodName = hsRequest.getMethod(); final HttpMethod method; if ("POST".equalsIgnoreCase(methodName)) { PostMethod postMethod = new PostMethod(); InputStreamRequestEntity inputStreamRequestEntity = new InputStreamRequestEntity( hsRequest.getInputStream()); postMethod.setRequestEntity(inputStreamRequestEntity); method = postMethod;//from ww w .j a v a 2s . co m } else if ("GET".equalsIgnoreCase(methodName)) { method = new GetMethod(); } else { // log.warn("Unsupported HTTP method requested: " + hsRequest.getMethod()); return null; } method.setFollowRedirects(false); method.setPath(targetUrl.getPath()); method.setQueryString(targetUrl.getQuery()); @SuppressWarnings("unchecked") Enumeration<String> e = hsRequest.getHeaderNames(); if (e != null) { while (e.hasMoreElements()) { String headerName = e.nextElement(); if ("host".equalsIgnoreCase(headerName)) { //the host value is set by the http client continue; } else if ("content-length".equalsIgnoreCase(headerName)) { //the content-length is managed by the http client continue; } else if ("accept-encoding".equalsIgnoreCase(headerName)) { //the accepted encoding should only be those accepted by the http client. //The response stream should (afaik) be deflated. If our http client does not support //gzip then the response can not be unzipped and is delivered wrong. continue; } else if (headerName.toLowerCase().startsWith("cookie")) { //fixme : don't set any cookies in the proxied request, this needs a cleaner solution continue; } @SuppressWarnings("unchecked") Enumeration<String> values = hsRequest.getHeaders(headerName); while (values.hasMoreElements()) { String headerValue = values.nextElement(); // log.info("setting proxy request parameter:" + headerName + ", value: " + headerValue); method.addRequestHeader(headerName, headerValue); } } } // add rs5/tomcat5 request header for ML method.addRequestHeader("X-Via", "tomcat5"); // log.info("proxy query string " + method.getQueryString()); return method; }
From source file:it.marcoberri.mbmeteo.helper.ConfigurationHelper.java
private ConfigurationHelper() { try {/*w w w. j a v a2s . c o 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"))); } catch (final IOException e) { throw new RuntimeException("Error initializing mongo db", e); } }
From source file:com.scvngr.levelup.core.net.request.factory.LocationCreditRequestFactoryTest.java
/** * Tests building a location credit request. *//* w w w. j av a 2 s . c o m*/ @SmallTest public void testBuildLocationCreditRequest_noToken() throws AbstractRequest.BadRequestException, IOException { final LocationCreditRequestFactory builder = new LocationCreditRequestFactory(getContext(), new MockAccessTokenRetriever(null)); final AbstractRequest request = builder .buildLocationCreditRequest(LocationFixture.getFullModel(LOCATION_ID_FIXTURE)); assertNotNull(request); final URL url = request.getUrl(getContext()); assertTrue("hits /locations/:id/credit endpoint", url.getPath().contains("/locations/")); assertTrue("hits /locations/:id/credit endpoint", url.getPath().contains("/credit")); assertFalse(request.getRequestHeaders(getContext()).containsKey(LevelUpRequest.HEADER_AUTHORIZATION)); assertEquals(RequestUtils.HEADER_CONTENT_TYPE_JSON, request.getRequestHeaders(getContext()).get(HTTP.CONTENT_TYPE)); assertEquals(HttpMethod.GET, request.getMethod()); assertTrue(url.getPath().startsWith("/v15")); }
From source file:com.scvngr.levelup.core.net.request.factory.LocationCreditRequestFactoryTest.java
/** * Tests building a location credit request. *//*from ww w . j a va 2s . c om*/ @SmallTest public void testBuildLocationCreditRequest_withToken() throws AbstractRequest.BadRequestException, IOException { final LocationCreditRequestFactory builder = new LocationCreditRequestFactory(getContext(), new MockAccessTokenRetriever()); final AbstractRequest request = builder .buildLocationCreditRequest(LocationFixture.getFullModel(LOCATION_ID_FIXTURE)); assertNotNull(request); final URL url = request.getUrl(getContext()); assertTrue("hits /locations/:id/credit endpoint", url.getPath().contains("/locations/")); assertTrue("hits /locations/:id/credit endpoint", url.getPath().contains("/credit")); assertTrue(request.getRequestHeaders(getContext()).containsKey(LevelUpRequest.HEADER_AUTHORIZATION)); assertEquals(RequestUtils.HEADER_CONTENT_TYPE_JSON, request.getRequestHeaders(getContext()).get(HTTP.CONTENT_TYPE)); assertEquals(HttpMethod.GET, request.getMethod()); assertTrue(url.getPath().startsWith("/v15")); }
From source file:com.tasktop.c2c.server.jenkins.configuration.service.JenkinsWarDeprovisioner.java
@Override public void deprovision(ProjectServiceConfiguration configuration) { try {// ww w . jav a2 s . c o m String deployedUrl = configuration.getProperties().get(ProjectServiceConfiguration.PROFILE_BASE_URL) + jenkinsPath + (perOrg ? configuration.getOrganizationIdentifier() : configuration.getProjectIdentifier()) + "/jenkins/"; deployedUrl.replace("//", "/"); URL deployedJenkinsUrl = new URL(deployedUrl); String webappName = deployedJenkinsUrl.getPath(); if (webappName.startsWith("/")) { webappName = webappName.substring(1); } if (webappName.endsWith("/")) { webappName = webappName.substring(0, webappName.length() - 1); } webappName = webappName.replace("/", "#"); tryDelete(new File(jenkinsWebappsDir, webappName)); tryDelete(new File(jenkinsWebappsDir, webappName + ".war")); } catch (MalformedURLException e) { throw new RuntimeException(e); } }
From source file:org.jcodec.player.filters.http.HttpMedia.java
public HttpMedia(URL url, File cacheWhere) throws IOException { cacheWhere = new File(cacheWhere, url.getHost() + "_" + url.getPath().replace("/", "_")); String data = requestInfo(url, getHttpClient(url.toExternalForm())); MediaInfo[] mediaInfos = MediaInfoParser.parseMediaInfos(data); for (int i = 0; i < mediaInfos.length; i++) { if (mediaInfos[i] == null) continue; try {//from ww w. j a v a 2s . c o m HttpPacketSource ps = new HttpPacketSource(url.toExternalForm() + "/" + i, new File(cacheWhere + "_" + i), mediaInfos[i]); tracks.add(ps); if (mediaInfos[i] instanceof VideoInfo) videoTrack = ps; else audioTracks.add(ps); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.adito.core.RequestParameterMap.java
private static URL parseProxiedURL(String location, MultiMap map) { try {//from w w w . ja v a 2 s .c o m URL proxiedURL = new URL(location); URL proxiedURLBase = proxiedURL; // Extract parameters from the proxied URL String query = proxiedURL.getQuery(); if (query != null) { proxiedURLBase = new URL(proxiedURL.getProtocol(), proxiedURL.getHost(), proxiedURL.getPort() < 1 ? -1 : proxiedURL.getPort(), proxiedURL.getPath()); parseQuery(map, query); } return proxiedURLBase; } catch (MalformedURLException murle) { log.error("Invalid proxied URL '" + location + "'"); } return null; }
From source file:net.sf.jabref.help.HelpContent.java
/** * Convenience method for setPage(String) *//* w w w. j a va 2 s.c o m*/ public void setPage(URL url) { File f = new File(url.getPath()); setPage(f.getName(), JabRef.class); }
From source file:free.yhc.feeder.model.Utils.java
public static String getExtentionFromUrl(String url) { /*// w ww.j a v a2 s .co m * Sometimes MimeTypeMap.getFileExtentionFromUrl returns null event if url is valid * (Especially, in case that URL contains Korean). * So, DO NOT USE it! String ext = MimeTypeMap.getFileExtensionFromUrl(url); if (isValidValue(ext)) return ext; */ URL u; try { u = new URL(url); } catch (MalformedURLException e) { return ""; } String path = u.getPath(); int i = path.lastIndexOf('.'); if (i < 0) return ""; try { return path.substring(i + 1); } catch (IndexOutOfBoundsException e) { return ""; } }
From source file:org.dkpro.lab.Util.java
/** * Make the given URL available as a file. A temporary file is created and * deleted upon a regular shutdown of the JVM. If the parameter {@code * aCache} is {@code true}, the temporary file is remembered in a cache and * if a file is requested for the same URL at a later time, the same file is * returned again. If the previously created file has been deleted * meanwhile, it is recreated from the URL. * * @param aUrl//from ww w. j av a 2 s. co m * the URL. * @param aCache * use the cache or not. * @return a file created from the given URL. * @throws IOException * if the URL cannot be accessed to (re)create the file. */ public static synchronized File getUrlAsFile(URL aUrl, boolean aCache) throws IOException { // If the URL already points to a file, there is not really much to do. if ("file".equals(aUrl.getProtocol())) { try { return new File(aUrl.toURI()); } catch (URISyntaxException e) { throw new IOException(e); } } // Lets see if we already have a file for this URL in our cache. Maybe // the file has been deleted meanwhile, so we also check if the file // actually still exists on disk. File file = urlFileCache.get(aUrl); if (!aCache || (file == null) || !file.exists()) { // Create a temporary file and try to preserve the file extension String suffix = ".temp"; String name = new File(aUrl.getPath()).getName(); int suffixSep = name.indexOf("."); if (suffixSep != -1) { suffix = name.substring(suffixSep + 1); name = name.substring(0, suffixSep); } // Get a temporary file which will be deleted when the JVM shuts // down. file = File.createTempFile(name, suffix); file.deleteOnExit(); // Now copy the file from the URL to the file. shoveAndClose(aUrl.openStream(), new FileOutputStream(file)); // Remember the file if (aCache) { urlFileCache.put(aUrl, file); } } return file; }