List of usage examples for java.net URI toURL
public URL toURL() throws MalformedURLException
From source file:net.di2e.ecdr.commons.CDRMetacard.java
@Override public byte[] getThumbnail() { byte[] thumbnail = null; // We can't call getData here because it will get in an endless loop because of the explicit checks in // getAttribute (for THUMBNAIL), so instead we check the values ourselves then try to pull from the link (on // demand)/*from ww w .j a v a2s. c om*/ Attribute thumbnailAttribute = (wrappedMetacard != null) ? wrappedMetacard.getAttribute(THUMBNAIL) : map.get(THUMBNAIL); if (thumbnailAttribute == null || getAttributeValue(thumbnailAttribute, byte[].class) == null) { URI thumbnailURI = getThumbnailURL(); if (thumbnailURI != null) { try (InputStream in = thumbnailURI.toURL().openStream()) { thumbnail = IOUtils.toByteArray(in); setAttribute(new AttributeImpl(Metacard.THUMBNAIL, thumbnail)); } catch (MalformedURLException e) { LOGGER.warn("Cannot read thumbnail due to invalid thumbnail URL[" + thumbnailURI + "]: " + e.getMessage(), e); } catch (IOException e) { LOGGER.warn("Could not read thumbnail from remote URL[" + thumbnailURI + "] due to: " + e.getMessage(), e); } } } else { thumbnail = getAttributeValue(thumbnailAttribute, byte[].class); } return thumbnail; }
From source file:net.di2e.ecdr.commons.CDRMetacard.java
@Override public String getMetadata() { String metadata = null;/*from w ww . j a v a 2s. com*/ // We can't call getString here because it will get in an endless loop because of the explicit checks in // getAttribute (for METADATA), so instead we check the values ourselves then try to pull from the link (on // demand) Attribute metadataAttribute = (wrappedMetacard != null) ? wrappedMetacard.getAttribute(METADATA) : map.get(METADATA); if (metadataAttribute == null || StringUtils.isBlank(getAttributeValue(metadataAttribute, String.class))) { URI metadataURI = getMetadataURL(); if (metadataURI != null) { try (InputStream in = metadataURI.toURL().openStream()) { metadata = IOUtils.toString(in); if (getAttribute(CDRMetacard.WRAP_METADATA) != null) { StringBuilder sb = new StringBuilder(); sb.append("<xml>"); sb.append(metadata); sb.append("</xml>"); metadata = sb.toString(); } setAttribute(new AttributeImpl(Metacard.METADATA, metadata)); } catch (MalformedURLException e) { LOGGER.warn("Cannot read metadata due to Invalid metadata URL[" + metadataURI + "]: " + e.getMessage(), e); } catch (IOException e) { LOGGER.warn("Could not read metadata from remote URL[" + metadataURI + "] due to: " + e.getMessage(), e); } } } else { metadata = getAttributeValue(metadataAttribute, String.class); } return metadata; }
From source file:io.swagger.api.impl.ToolsApiServiceImpl.java
@Override public Response toolsGet(String registryId, String registry, String organization, String name, String toolname, String description, String author, String offset, Integer limit, SecurityContext securityContext) throws NotFoundException { final List<Entry> all = new ArrayList<>(); all.addAll(toolDAO.findAllPublished()); all.addAll(workflowDAO.findAllPublished()); all.sort((o1, o2) -> o1.getGitUrl().compareTo(o2.getGitUrl())); List<io.swagger.model.Tool> results = new ArrayList<>(); for (Entry c : all) { if (c instanceof Workflow && (registryId != null || registry != null || organization != null || name != null || toolname != null)) { continue; }/*ww w .ja v a2s .co m*/ if (c instanceof Tool) { Tool tool = (Tool) c; // check each criteria. This sucks. Can we do this better with reflection? Or should we pre-convert? if (registryId != null) { if (!registryId.contains(tool.getToolPath())) { continue; } } if (registry != null && tool.getRegistry() != null) { if (!tool.getRegistry().toString().contains(registry)) { continue; } } if (organization != null && tool.getNamespace() != null) { if (!tool.getNamespace().contains(organization)) { continue; } } if (name != null && tool.getName() != null) { if (!tool.getName().contains(name)) { continue; } } if (toolname != null && tool.getToolname() != null) { if (!tool.getToolname().contains(toolname)) { continue; } } } if (description != null && c.getDescription() != null) { if (!c.getDescription().contains(description)) { continue; } } if (author != null && c.getAuthor() != null) { if (!c.getAuthor().contains(author)) { continue; } } // if passing, for each container that matches the criteria, convert to standardised format and return io.swagger.model.Tool tool = convertContainer2Tool(c).getLeft(); if (tool != null) { results.add(tool); } } if (limit == null) { limit = DEFAULT_PAGE_SIZE; } List<List<io.swagger.model.Tool>> pagedResults = Lists.partition(results, limit); int offsetInteger = 0; if (offset != null) { offsetInteger = Integer.parseInt(offset); } if (offsetInteger >= pagedResults.size()) { results = new ArrayList<>(); } else { results = pagedResults.get(offsetInteger); } final Response.ResponseBuilder responseBuilder = Response.ok(results); responseBuilder.header("current-offset", offset); responseBuilder.header("current-limit", limit); // construct links to other pages try { List<String> filters = new ArrayList<>(); handleParameter(registryId, "id", filters); handleParameter(organization, "organization", filters); handleParameter(name, "name", filters); handleParameter(toolname, "toolname", filters); handleParameter(description, "description", filters); handleParameter(author, "author", filters); handleParameter(registry, "registry", filters); handleParameter(limit.toString(), "limit", filters); if (offsetInteger + 1 < pagedResults.size()) { URI nextPageURI = new URI(config.getScheme(), null, config.getHostname(), Integer.parseInt(config.getPort()), "/api/ga4gh/v1/tools", Joiner.on('&').join(filters) + "&offset=" + (offsetInteger + 1), null); responseBuilder.header("next-page", nextPageURI.toURL().toString()); } URI lastPageURI = new URI(config.getScheme(), null, config.getHostname(), Integer.parseInt(config.getPort()), "/api/ga4gh/v1/tools", Joiner.on('&').join(filters) + "&offset=" + (pagedResults.size() - 1), null); responseBuilder.header("last-page", lastPageURI.toURL().toString()); } catch (URISyntaxException | MalformedURLException e) { throw new WebApplicationException("Could not construct page links", HttpStatus.SC_BAD_REQUEST); } return responseBuilder.build(); }
From source file:nl.coinsweb.sdk.FileManager.java
/** * Return either an url or a file:// url to find the resource * @param internalRef//from w ww . j a va 2 s .c om * @param resource * @return */ public static URI getLibrary(JenaCoinsContainer container, String internalRef, URI resource) { log.trace("get library " + resource + " in " + internalRef); try { File file = new File(resource); if (file.exists()) { log.trace("found file as local path " + resource); copyAndLinkLibrary(internalRef, file); return file.toURI(); } } catch (IllegalArgumentException e) { // try next option, the resource is not a file } Namespace resourceAsNs = null; try { resourceAsNs = new Namespace(resource.toString()); } catch (InvalidNamespaceException e) { } if (resourceAsNs != null) { if (internalRef != null && container != null) { if (container.getAvailableLibraryFiles().containsKey(resourceAsNs)) { File matchingFile = container.getAvailableLibraryFiles().get(resourceAsNs); log.trace("found file as previously registered " + matchingFile.getAbsolutePath()); copyAndLinkLibrary(internalRef, matchingFile); return matchingFile.toURI(); } } } // Alternatively try to resolve the resourceUri online try { URL resourceAsUrl = resource.toURL(); HttpURLConnection connection = (HttpURLConnection) resourceAsUrl.openConnection(); connection.setRequestMethod("HEAD"); int responseCode = connection.getResponseCode(); if (responseCode == 200) { log.info("found active link online: " + resource); return resource; } } catch (MalformedURLException e) { } catch (ProtocolException e) { } catch (IOException e) { } log.trace("Import resource can not be found in the ccr and is not available online."); throw new CoinsResourceNotFoundException( "Import resource can not be found in the ccr and is not available online."); }
From source file:org.openxrd.discovery.impl.HtmlLinkDiscoveryMethod.java
/** {@inheritDoc} */ public URI getXRDLocation(URI uri) throws DiscoveryException { try {/*w w w .j av a2 s. co m*/ HttpResponse response = fetch(uri); HttpEntity entity = response.getEntity(); if (entity == null) { entity.consumeContent(); return null; } String content = EntityUtils.toString(entity); Parser htmlParser = Parser.createParser(content, null); LinkVisitor linkVisitor = new LinkVisitor(); htmlParser.visitAllNodesWith(linkVisitor); for (Tag tag : linkVisitor.getLinks()) { if (!XRDConstants.XRD_MIME_TYPE.equals(tag.getAttribute("type"))) { continue; } if (!XRDConstants.XRD_REL_DESCRIBEDBY.equalsIgnoreCase(tag.getAttribute("rel"))) { continue; } try { URL xrdLocation = new URL(uri.toURL(), tag.getAttribute("href")); LOG.debug("Found XRD location: {}", xrdLocation.toString()); return xrdLocation.toURI(); } catch (URISyntaxException e) { continue; } } return null; } catch (IOException e) { throw new DiscoveryException(e); } catch (ParserException e) { throw new DiscoveryException(e); } }
From source file:com.twilio.sdk.AppEngineClientConnection.java
public void sendRequestHeader(HttpRequest request) throws HttpException, IOException { try {//from w w w .j ava 2s .c o m HttpHost host = route.getTargetHost(); URI uri = new URI(host.getSchemeName() + "://" + host.getHostName() + ((host.getPort() == -1) ? "" : (":" + host.getPort())) + request.getRequestLine().getUri()); Class[] requestConstructorTypes = new Class[3]; requestConstructorTypes[0] = URL.class; requestConstructorTypes[1] = HTTPMethod; requestConstructorTypes[2] = FetchOptions; Constructor requestConstructor = HTTPRequest.getConstructor(requestConstructorTypes); Class[] disallowTruncateTypes = new Class[0]; Method disallowTruncate = FetchOptionsBuilder.getMethod("disallowTruncate", disallowTruncateTypes); this.request = requestConstructor.newInstance(uri.toURL(), Enum.valueOf( (Class<? extends Enum>) Class.forName("com.google.appengine.api.urlfetch.HTTPMethod"), request.getRequestLine().getMethod()), disallowTruncate.invoke(FetchOptionsBuilder, new Object[0])); Class[] addHeaderParameterTypes = new Class[1]; addHeaderParameterTypes[0] = HTTPHeader; Method addHeader = HTTPRequest.getMethod("addHeader", addHeaderParameterTypes); Class[] httpHeaderConstructorTypes = new Class[2]; httpHeaderConstructorTypes[0] = String.class; httpHeaderConstructorTypes[1] = String.class; Constructor httpHeaderConstructor = HTTPHeader.getConstructor(httpHeaderConstructorTypes); for (Header h : request.getAllHeaders()) { addHeader.invoke(this.request, httpHeaderConstructor.newInstance(h.getName(), h.getValue())); } } catch (Exception e) { throw new IOException("Error during invocation: " + e.getMessage(), e); } }
From source file:com.heaptrip.util.http.bixo.fetcher.SimpleHttpFetcher.java
private FetchedResult doRequest(HttpRequestBase request, String url, List<TupleTwo<?, ?>> data, List<TupleTwo<?, ?>> headers) throws BaseFetchException { LOGGER.trace("Fetching " + url); HttpResponse response;//from w w w. j a v a 2s .c o m long readStartTime; HttpHeaders headerMap = new HttpHeaders(); String redirectedUrl = null; String newBaseUrl = null; int numRedirects = 0; boolean needAbort = true; String contentType = ""; String hostAddress = null; // Create a local instance of cookie store, and bind to local context // Without this we get killed w/lots of threads, due to sync() on single // cookie store. HttpContext localContext = new BasicHttpContext(); CookieStore cookieStore = new BasicCookieStore(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); try { URI uri = new URI(url); request.setURI(uri); request.setHeader("Host", uri.getHost()); if (headers != null) { for (TupleTwo<?, ?> t : headers) { request.setHeader(t.getKey().toString(), t.getValue().toString()); } } //collect post data if available if (request instanceof HttpPost && data != null) { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); for (TupleTwo<?, ?> e : data) { nameValuePairs.add(new BasicNameValuePair(URLEncoder.encode(e.getKey().toString(), "utf-8"), URLEncoder.encode(e.getValue().toString(), "utf-8"))); } ((HttpPost) (request)).setEntity(new UrlEncodedFormEntity(nameValuePairs)); } readStartTime = System.currentTimeMillis(); response = _httpClient.execute(request, localContext); Header[] responseHeaders = response.getAllHeaders(); for (Header header : responseHeaders) { headerMap.add(header.getName(), header.getValue()); } int httpStatus = response.getStatusLine().getStatusCode(); if ((httpStatus < 200) || (httpStatus >= 300)) { // We can't just check against SC_OK, as some wackos return 201, 202, // etc throw new HttpFetchException(url, "Error fetching " + url + " due to http status code " + httpStatus, httpStatus, headerMap); } redirectedUrl = extractRedirectedUrl(url, localContext); URI permRedirectUri = (URI) localContext.getAttribute(PERM_REDIRECT_CONTEXT_KEY); if (permRedirectUri != null) { newBaseUrl = permRedirectUri.toURL().toExternalForm(); } Integer redirects = (Integer) localContext.getAttribute(REDIRECT_COUNT_CONTEXT_KEY); if (redirects != null) { numRedirects = redirects.intValue(); } hostAddress = (String) (localContext.getAttribute(HOST_ADDRESS)); if (hostAddress == null) { throw new UrlFetchException(url, "Host address not saved in context"); } Header cth = response.getFirstHeader(HttpHeaderNames.CONTENT_TYPE); if (cth != null) { contentType = cth.getValue(); } needAbort = false; } catch (ClientProtocolException e) { // Oleg guarantees that no abort is needed in the case of an IOException // (which is is a subclass of) needAbort = false; // If the root case was a "too many redirects" error, we want to map this // to a specific // exception that contains the final redirect. if (e.getCause() instanceof MyRedirectException) { MyRedirectException mre = (MyRedirectException) e.getCause(); String redirectUrl = url; try { redirectUrl = mre.getUri().toURL().toExternalForm(); } catch (MalformedURLException e2) { LOGGER.warn("Invalid URI saved during redirect handling: " + mre.getUri()); } throw new RedirectFetchException(url, redirectUrl, mre.getReason()); } else if (e.getCause() instanceof RedirectException) { throw new RedirectFetchException(url, extractRedirectedUrl(url, localContext), RedirectExceptionReason.TOO_MANY_REDIRECTS); } else { throw new IOFetchException(url, e); } } catch (IOException e) { // Oleg guarantees that no abort is needed in the case of an IOException needAbort = false; if (e instanceof ConnectionPoolTimeoutException) { // Should never happen, so let's dump some info about the connection // pool. ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) _httpClient.getConnectionManager(); int numConnections = cm.getConnectionsInPool(); cm.closeIdleConnections(0, TimeUnit.MILLISECONDS); LOGGER.error(String.format( "Got ConnectionPoolTimeoutException: %d connections before, %d after idle close", numConnections, cm.getConnectionsInPool())); } throw new IOFetchException(url, e); } catch (URISyntaxException e) { throw new UrlFetchException(url, e.getMessage()); } catch (IllegalStateException e) { throw new UrlFetchException(url, e.getMessage()); } catch (BaseFetchException e) { throw e; } catch (Exception e) { // Map anything else to a generic IOFetchException // TODO KKr - create generic fetch exception throw new IOFetchException(url, new IOException(e)); } finally { safeAbort(needAbort, request); } // Figure out how much data we want to try to fetch. int targetLength = _fetcherPolicy.getMaxContentSize(); boolean truncated = false; String contentLengthStr = headerMap.getFirst(HttpHeaderNames.CONTENT_LENGTH); if (contentLengthStr != null) { try { int contentLength = Integer.parseInt(contentLengthStr); if (contentLength > targetLength) { truncated = true; } else { targetLength = contentLength; } } catch (NumberFormatException e) { // Ignore (and log) invalid content length values. LOGGER.warn("Invalid content length in header: " + contentLengthStr); } } // Now finally read in response body, up to targetLength bytes. // Note that entity might be null, for zero length responses. byte[] content = new byte[0]; long readRate = 0; HttpEntity entity = response.getEntity(); needAbort = true; if (entity != null) { InputStream in = null; try { in = entity.getContent(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = 0; int totalRead = 0; ByteArrayOutputStream out = new ByteArrayOutputStream(DEFAULT_BYTEARRAY_SIZE); int readRequests = 0; int minResponseRate = _fetcherPolicy.getMinResponseRate(); // TODO KKr - we need to monitor the rate while reading a // single block. Look at HttpClient // metrics support for how to do this. Once we fix this, fix // the test to read a smaller (< 20K) // chuck of data. while ((totalRead < targetLength) && ((bytesRead = in.read(buffer, 0, Math.min(buffer.length, targetLength - totalRead))) != -1)) { readRequests += 1; totalRead += bytesRead; out.write(buffer, 0, bytesRead); // Assume read time is at least one millisecond, to avoid DBZ // exception. long totalReadTime = Math.max(1, System.currentTimeMillis() - readStartTime); readRate = (totalRead * 1000L) / totalReadTime; // Don't bail on the first read cycle, as we can get a hiccup starting // out. // Also don't bail if we've read everything we need. if ((readRequests > 1) && (totalRead < targetLength) && (readRate < minResponseRate)) { throw new AbortedFetchException(url, "Slow response rate of " + readRate + " bytes/sec", AbortedFetchReason.SLOW_RESPONSE_RATE); } // Check to see if we got interrupted. if (Thread.interrupted()) { throw new AbortedFetchException(url, AbortedFetchReason.INTERRUPTED); } } content = out.toByteArray(); needAbort = truncated || (in.available() > 0); } catch (IOException e) { // We don't need to abort if there's an IOException throw new IOFetchException(url, e); } finally { safeAbort(needAbort, request); safeClose(in); } } return new FetchedResult(url, redirectedUrl, System.currentTimeMillis(), headerMap, content, contentType, (int) readRate, newBaseUrl, numRedirects, hostAddress); }
From source file:de.bps.course.nodes.vc.provider.wimba.WimbaClassroomProvider.java
@Override public URL createClassroomGuestUrl(String roomId, Identity identity, VCConfiguration config) { URL url = null;/* www. ja v a 2 s . c om*/ URI uri = UriBuilder.fromUri(protocol + "://" + baseUrl).port(port).path("launcher.cgi") .queryParam("room", PREFIX + roomId).build(); try { url = uri.toURL(); } catch (MalformedURLException e) { logWarn("Cannot create guest access URL to Wimba Classroom meeting for id \"" + PREFIX + roomId, e); } return url; }
From source file:com.sastix.cms.server.services.content.impl.HashedDirectoryServiceImpl.java
@Override public String storeFile(final String UURI, final String tenantID, final URI resourceURI) throws IOException { //Create Unique hash final String hash = hashText(UURI); if (hash == null) { //TODO: Throw exception LOG.error("Unable to create HASH for UID {}", UURI); }/*from w w w .ja v a 2 s .com*/ //Get Filename (including directories) final String filename = getFilename(hash); //Add Volume to the Path final Path file = Paths.get(VOLUME + "/" + tenantID + filename); //Create the directories. Files.createDirectories(file.getParent()); //Convert URI to URL final URL resourceUrl = resourceURI.toURL(); //Write the contents to the file writeFile(file, resourceUrl); return getRelativePath(file); }