List of usage examples for java.net URI toURL
public URL toURL() throws MalformedURLException
From source file:de.mpg.imeji.presentation.metadata.extractors.BasicExtractor.java
public static List<String> extractTechMd(Item item) throws Exception { List<String> techMd = new ArrayList<String>(); URI uri = item.getFullImageUrl(); String imageUrl = uri.toURL().toString(); GetMethod method = new GetMethod(imageUrl); method.setFollowRedirects(false);// www . j a v a2s.co m String userHandle = null; userHandle = LoginHelper.login(PropertyReader.getProperty("imeji.escidoc.user"), PropertyReader.getProperty("imeji.escidoc.password")); method.addRequestHeader("Cookie", "escidocCookie=" + userHandle); HttpClient client = new HttpClient(); client.executeMethod(method); InputStream input = method.getResponseBodyAsStream(); ImageInputStream iis = ImageIO.createImageInputStream(input); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (readers.hasNext()) { // pick the first available ImageReader ImageReader reader = readers.next(); // attach source to the reader reader.setInput(iis, true); // read metadata of first image IIOMetadata metadata = reader.getImageMetadata(0); String[] names = metadata.getMetadataFormatNames(); int length = names.length; for (int i = 0; i < length; i++) { displayMetadata(techMd, metadata.getAsTree(names[i])); } } return techMd; }
From source file:Main.java
private static String escapeUrlString(String urlString) throws MalformedURLException, URISyntaxException { URL url = new URL(urlString); URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toURL().toString(); }
From source file:eu.eubrazilcc.lvl.storage.urlshortener.UrlShortener.java
private static final String normalizeUrl(final String url) { String normlizedUrl = url;// w w w . j a va2 s .co m try { final URI uri = new URI(url).normalize(); normlizedUrl = uri.toURL().toString(); } catch (Exception e) { LOGGER.error("Failed to normalize URL: " + url, e); } return normlizedUrl; }
From source file:io.github.medinam.jcheesum.util.VersionChecker.java
private static JSONTokener getVersionTokener() throws URISyntaxException, MalformedURLException, IOException { URI uri = new URI("https://medinam.github.io/jcheesum/version.json"); return new JSONTokener(uri.toURL().openConnection().getInputStream()); }
From source file:tech.sirwellington.alchemy.http.VerbAssertions.java
private static void assertRequestWith(HttpVerb verb, Class<? extends HttpUriRequest> type) throws Exception { HttpClient mockClient = mock(HttpClient.class); when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(createFakeApacheResponse()); URI uri = createFakeUri(); HttpRequest request = HttpRequest.Builder.newInstance().usingUrl(uri.toURL()).build(); verb.execute(mockClient, request);/*from w w w. j ava 2 s . co m*/ ArgumentCaptor<HttpUriRequest> captor = forClass(HttpUriRequest.class); verify(mockClient).execute(captor.capture()); HttpUriRequest requestMade = captor.getValue(); assertThat(requestMade, notNullValue()); assertThat(requestMade.getURI(), is(uri)); assertThat(requestMade.getClass(), sameInstance(type)); }
From source file:edu.stanford.base.batch.RawCollectionsExample.java
/** * @param subcat// www. j a va 2 s . c o m * @throws IOException * @throws HttpException * @throws JsonParseException * @throws URISyntaxException */ public static void pullSearsProducts(String subcat) throws IOException, HttpException, JsonParseException, URISyntaxException { StringBuffer reponse = new StringBuffer(); //http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&searchType=category&productsOnly=1&contentType=json //String request = "http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&subCategoryName=Side-by-Side+Refrigerators&searchType=subcategory&productsOnly=1&endIndex=1000&startIndex=1&contentType=json"; String request = "http://api.developer.sears.com/v1/productsearch?apikey=09b9aeb25ff985a9c85d877f8a9c4dd9&store=Sears&verticalName=Appliances&categoryName=Refrigerators&subCategoryName=" + subcat + "&searchType=subcategory&productsOnly=1&contentType=json&endIndex=1000&startIndex=1"; URI uri = new URI(request); URL url = uri.toURL(); //Compact+Refrigerators System.out.println(url); HttpClient client = new HttpClient(); GetMethod method = new GetMethod(request); // Send GET request int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } InputStream rstream = null; // Get the response body rstream = method.getResponseBodyAsStream(); // Process the response from Yahoo! Web Services BufferedReader br = new BufferedReader(new InputStreamReader(rstream)); String line; while ((line = br.readLine()) != null) { reponse.append(line); System.out.println(line); } br.close(); Gson gson = new Gson(); /* // gson.registerTypeAdapter(Event.class, new MyInstanceCreator()); Collection collection = new ArrayList(); collection.add("hello"); collection.add(5); collection.add(new Event("GREETINGS", "guest")); String json2 = gson.toJson(collection); System.out.println("Using Gson.toJson() on a raw collection: " + json2);*/ JsonParser parser = new JsonParser(); //JsonArray array = parser.parse(json1).getAsJsonArray(); String products = StringUtils.remove(reponse.toString(), "{\"mercadoresult\":{\"products\":{\"product\":[true,"); //System.out.println(products); String productsList = StringUtils.substring(products, 0, StringUtils.indexOf(products, "productcount") - 3); // System.out.println(productsList); productsList = "[" + StringUtils.replaceOnce(productsList, "}}]]", "}]]"); //System.out.println(productsList); List<SearsProduct> prodList = new ArrayList<SearsProduct>(); // Reader reader = new InputStreamReader(productsList); Gson gson1 = new GsonBuilder().create(); JsonArray array1 = parser.parse(productsList).getAsJsonArray(); JsonArray prodArray = (JsonArray) array1.get(0); // prodList= gson1.fromJson(array1.get(2), ArrayList.class); for (JsonElement jsonElement : prodArray) { prodList.add(gson.fromJson(jsonElement, SearsProduct.class)); //System.out.println(gson.fromJson(jsonElement, SearsProduct.class)); } PullSearsProductsDAO pullSearsProductsDAO = new PullSearsProductsDAO(); try { pullSearsProductsDAO.pullAllProducts(prodList); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static URI unredirect(URI uri) throws IOException { if (!REDIRECTOR_DOMAINS.contains(uri.getHost())) { return uri; }/* w w w .ja va2s . co m*/ URL url = uri.toURL(); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setInstanceFollowRedirects(false); connection.setDoInput(false); connection.setRequestMethod("HEAD"); connection.setRequestProperty("User-Agent", "ZXing (Android)"); try { connection.connect(); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_MULT_CHOICE: case HttpURLConnection.HTTP_MOVED_PERM: case HttpURLConnection.HTTP_MOVED_TEMP: case HttpURLConnection.HTTP_SEE_OTHER: case 307: // No constant for 307 Temporary Redirect ? String location = connection.getHeaderField("Location"); if (location != null) { try { return new URI(location); } catch (URISyntaxException e) { // nevermind } } } return uri; } finally { connection.disconnect(); } }
From source file:com.google.cloud.runtime.jetty.util.HttpUrlUtil.java
/** * Open a new {@link HttpURLConnection} to the provided URI. * <p>/*from w w w.ja v a2 s . c om*/ * Note: will also set the 'User-Agent' to {@code jetty-runtime/gcloud-util-core} * </p> * * @param uri the URI to open to * @return the open HttpURLConnection * @throws IOException if unable to open the connection */ public static HttpURLConnection openTo(URI uri) throws IOException { log.info("HttpUrlUtil.openTo(" + uri + ")"); HttpURLConnection http = (HttpURLConnection) uri.toURL().openConnection(); http.setRequestProperty("User-Agent", "jetty-runtime/gcloud-util-core"); return http; }
From source file:nl.flotsam.calendar.core.util.IcalWriter.java
private static Future<HTTPResponse> createFutureFrom(URI uri, URLFetchService urlFetchService) { try {/*from www .j a va 2 s. c o m*/ return urlFetchService.fetchAsync(uri.toURL()); } catch (MalformedURLException e) { return null; } }
From source file:org.eclairjs.nashorn.TestUtils.java
public static String resourceToFile(String resource) throws Exception { URI uri = TestUtils.class.getResource(resource).toURI(); File src = new File(uri); File dest = new File(System.getProperty("java.io.tmpdir"), FilenameUtils.getName(uri.toURL().toString())); FileUtils.copyFile(src, dest);//from w w w . j a v a 2 s. c om return dest.getAbsolutePath(); }