List of usage examples for java.net URLConnection getContentType
public String getContentType()
From source file:ch.entwine.weblounge.kernel.site.SiteServlet.java
/** * Tries to serve the request as a static resource from the bundle. * /* ww w. jav a 2 s . c om*/ * @param request * the http servlet request * @param response * the http servlet response * @throws ServletException * if serving the request fails * @throws IOException * if writing the response back to the client fails */ protected void serviceResource(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { Http11ResponseType responseType = null; String requestPath = request.getPathInfo(); // There is also a special set of resources that we don't want to expose if (isProtected(requestPath)) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } String bundlePath = UrlUtils.concat("/site", requestPath); // Does the resource exist? final URL url = bundle.getResource(bundlePath); if (url == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // Load the resource from the bundle URLConnection connection = url.openConnection(); String contentEncoding = connection.getContentEncoding(); long contentLength = connection.getContentLength(); long lastModified = connection.getLastModified(); if (contentLength <= 0) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return; } // final Resource resource = Resource.newResource(url); // if (!resource.exists()) { // response.sendError(HttpServletResponse.SC_NOT_FOUND); // return; // } // We don't allow directory listings // if (resource.isDirectory()) { // response.sendError(HttpServletResponse.SC_FORBIDDEN); // return; // } String mimeType = tika.detect(bundlePath); // Try to get mime type and content encoding from resource if (mimeType == null) mimeType = connection.getContentType(); if (mimeType != null) { if (contentEncoding != null) mimeType += ";" + contentEncoding; response.setContentType(mimeType); } // Send the response back to the client InputStream is = connection.getInputStream(); // InputStream is = resource.getInputStream(); try { logger.debug("Serving {}", url); responseType = Http11ProtocolHandler.analyzeRequest(request, lastModified, Times.MS_PER_DAY + System.currentTimeMillis(), contentLength); if (!Http11ProtocolHandler.generateResponse(response, responseType, is)) { logger.warn("I/O error while generating content from {}", url); } } finally { IOUtils.closeQuietly(is); } }
From source file:org.eclipse.rdf4j.repository.http.HTTPRepositoryConnection.java
public void add(URL url, String baseURI, RDFFormat dataFormat, Resource... contexts) throws IOException, RDFParseException, RepositoryException { if (baseURI == null) { baseURI = url.toExternalForm();//from w ww . j a va 2 s. com } URLConnection con = url.openConnection(); // Set appropriate Accept headers if (dataFormat != null) { for (String mimeType : dataFormat.getMIMETypes()) { con.addRequestProperty("Accept", mimeType); } } else { Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys(); List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, true, null); for (String acceptParam : acceptParams) { con.addRequestProperty("Accept", acceptParam); } } InputStream in = con.getInputStream(); if (dataFormat == null) { // Try to determine the data's MIME type String mimeType = con.getContentType(); int semiColonIdx = mimeType.indexOf(';'); if (semiColonIdx >= 0) { mimeType = mimeType.substring(0, semiColonIdx); } dataFormat = Rio.getParserFormatForMIMEType(mimeType).orElse( Rio.getParserFormatForFileName(url.getPath()).orElseThrow(Rio.unsupportedFormat(mimeType))); } try { add(in, baseURI, dataFormat, contexts); } finally { in.close(); } }
From source file:org.apache.velocity.tools.view.ImportSupport.java
/** * * @param url the URL to read/* ww w . j a va 2 s . co m*/ * @return a Reader for the InputStream created from the supplied URL * @throws IOException * @throws java.lang.Exception */ protected Reader acquireReader(String url) throws IOException, Exception { if (!isAbsoluteUrl(url)) { // for relative URLs, delegate to our peer return new StringReader(acquireString(url)); } else { // absolute URL URLConnection uc = null; HttpURLConnection huc = null; InputStream i = null; try { // handle absolute URLs ourselves, using java.net.URL URL u = new URL(url); // URL u = new URL("http", "proxy.hi.is", 8080, target); uc = u.openConnection(); i = uc.getInputStream(); // check response code for HTTP URLs, per spec, if (uc instanceof HttpURLConnection) { huc = (HttpURLConnection) uc; int status = huc.getResponseCode(); if (status < 200 || status > 299) { throw new Exception(status + " " + url); } } // okay, we've got a stream; encode it appropriately Reader r = null; String charSet; // charSet extracted according to RFC 2045, section 5.1 String contentType = uc.getContentType(); if (contentType != null) { charSet = ImportSupport.getContentTypeAttribute(contentType, "charset"); if (charSet == null) { charSet = DEFAULT_ENCODING; } } else { charSet = DEFAULT_ENCODING; } try { r = new InputStreamReader(i, charSet); } catch (UnsupportedEncodingException ueex) { r = new InputStreamReader(i, DEFAULT_ENCODING); } if (huc == null) { return r; } else { return new SafeClosingHttpURLConnectionReader(r, huc); } } catch (IOException ex) { if (i != null) { try { i.close(); } catch (IOException ioe) { LOG.error("Could not close InputStream", ioe); } } if (huc != null) { huc.disconnect(); } throw new Exception("Problem accessing the absolute URL \"" + url + "\". " + ex); } catch (RuntimeException ex) { if (i != null) { try { i.close(); } catch (IOException ioe) { LOG.error("Could not close InputStream", ioe); } } if (huc != null) { huc.disconnect(); } // because the spec makes us throw new Exception("Problem accessing the absolute URL \"" + url + "\". " + ex); } } }
From source file:org.clapper.curn.FeedDownloadThread.java
/** * Download a feed.//from w ww . j av a 2 s. c om * * @param conn the <tt>URLConnection</tt> for the feed * @param feedInfo the <tt>FeedInfo</tt> object for the feed * * @return the <tt>DownloadedTempFile</tt> object that captures the * details about the downloaded file * * @throws IOException I/O error * @throws CurnException some other error */ private DownloadedTempFile downloadFeed(final URLConnection conn, final FeedInfo feedInfo) throws CurnException, IOException { URL feedURL = feedInfo.getURL(); String feedURLString = feedURL.toString(); int totalBytes = 0; File tempFile = CurnUtil.createTempXMLFile(); log.debug("Downloading \"" + feedURLString + "\" to file \"" + tempFile.getPath()); InputStream urlStream = getURLInputStream(conn); /* Determine the character set encoding to use. * When downloading, all we are doing is copying bytes. * If we get an indication of encoding, we pass it along * to Rome or whatnot to use in interpreting those bytes. */ String protocol = feedURL.getProtocol(); String encoding = null; // null unless we see some other indication. if (protocol.equals("http") || protocol.equals("https")) { String contentTypeHeader = conn.getContentType(); if (contentTypeHeader != null) { encoding = contentTypeCharSet(contentTypeHeader); log.debug("HTTP server says encoding for \"" + feedURLString + "\" is \"" + ((encoding == null) ? "<null>" : encoding) + "\""); } } else if (protocol.equals("file")) { // Assume the same default encoding used by "SaveAsEncoding", // unless explicitly specified. encoding = Constants.DEFAULT_SAVE_AS_ENCODING; log.debug("Default encoding for \"" + feedURLString + "\" is \"" + encoding + "\""); } if (feedInfo.getForcedCharacterEncoding() != null) encoding = feedInfo.getForcedCharacterEncoding(); OutputStream tempOutput = null; try { tempOutput = new FileOutputStream(tempFile); totalBytes = IOUtils.copy(urlStream, tempOutput); } finally { IOUtils.closeQuietly(tempOutput); IOUtils.closeQuietly(urlStream); } // It's possible for totalBytes to be zero if, for instance, the // use of the If-Modified-Since header caused an HTTP server to // return no content. // It's possible for the encoding to be null if nothing gave us a clue. return new DownloadedTempFile(tempFile, encoding, totalBytes); }
From source file:com.servoy.j2db.util.Utils.java
public static String getURLContent(URL url) { StringBuffer sb = new StringBuffer(); String charset = null;/* www . j a va 2s.co m*/ try { URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); final String type = connection.getContentType(); if (type != null) { final String[] parts = type.split(";"); for (int i = 1; i < parts.length && charset == null; i++) { final String t = parts[i].trim(); final int index = t.toLowerCase().indexOf("charset="); if (index != -1) charset = t.substring(index + 8); } } InputStreamReader isr = null; if (charset != null) isr = new InputStreamReader(is, charset); else isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); int read = 0; while ((read = br.read()) != -1) { sb.append((char) read); } br.close(); isr.close(); is.close(); } catch (Exception e) { Debug.error(e); } return sb.toString(); }
From source file:bolts.WebViewAppLinkResolver.java
@Override public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) { final Capture<String> content = new Capture<String>(); final Capture<String> contentType = new Capture<String>(); return Task.callInBackground(new Callable<Void>() { @Override/*from w w w.j a va2 s .c o m*/ public Void call() throws Exception { URL currentURL = new URL(url.toString()); URLConnection connection = null; while (currentURL != null) { // Fetch the content at the given URL. connection = currentURL.openConnection(); if (connection instanceof HttpURLConnection) { // Unfortunately, this doesn't actually follow redirects if they go from http->https, // so we have to do that manually. ((HttpURLConnection) connection).setInstanceFollowRedirects(true); } connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) { currentURL = new URL(httpConnection.getHeaderField("Location")); httpConnection.disconnect(); } else { currentURL = null; } } else { currentURL = null; } } try { content.set(readFromConnection(connection)); contentType.set(connection.getContentType()); } finally { if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).disconnect(); } } return null; } }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() { @Override public Task<JSONArray> then(Task<Void> task) throws Exception { // Load the content in a WebView and use JavaScript to extract the meta tags. final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>(); final WebView webView = new WebView(context); webView.getSettings().setJavaScriptEnabled(true); webView.setNetworkAvailable(false); webView.setWebViewClient(new WebViewClient() { private boolean loaded = false; private void runJavaScript(WebView view) { if (!loaded) { // After the first resource has been loaded (which will be the pre-populated data) // run the JavaScript meta tag extraction script loaded = true; view.loadUrl(TAG_EXTRACTION_JAVASCRIPT); } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); runJavaScript(view); } @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); runJavaScript(view); } }); // Inject an object that will receive the JSON for the extracted JavaScript tags webView.addJavascriptInterface(new Object() { @JavascriptInterface public void setValue(String value) { try { tcs.trySetResult(new JSONArray(value)); } catch (JSONException e) { tcs.trySetError(e); } } }, "boltsWebViewAppLinkResolverResult"); String inferredContentType = null; if (contentType.get() != null) { inferredContentType = contentType.get().split(";")[0]; } webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null); return tcs.getTask(); } }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() { @Override public AppLink then(Task<JSONArray> task) throws Exception { Map<String, Object> alData = parseAlData(task.getResult()); AppLink appLink = makeAppLinkFromAlData(alData, url); return appLink; } }); }
From source file:org.wso2.carbon.repository.core.ResourceStorer.java
/** * Creates a resource with the content imported from the source URL and meta data extracted from * the given meta data resource instance. Then the created resource is put to the registry using * the Repository.put() method.//from w ww . j av a 2 s. com * * @param path Path to put the resource * @param sourceURL URL to import resource content * @param metaResource Meta data for the new resource is extracted from this meta data resource * * @return Actual path where the new resource is stored in the registry * @throws RepositoryException for all exceptional scenarios */ public String importResource(String path, String sourceURL, Resource metaResource) throws RepositoryException { String purePath = InternalUtils.getPureResourcePath(path); URL url; try { if (sourceURL == null || sourceURL.toLowerCase().startsWith("file:")) { String msg = "The source URL must not be file in the server's local file system"; throw new RepositoryException(msg); } url = new URL(sourceURL); } catch (MalformedURLException e) { String msg = "Given source URL is not valid."; throw new RepositoryException(msg, e); } try { URLConnection uc = url.openConnection(); InputStream in = uc.getInputStream(); String mediaType = metaResource.getMediaType(); if (mediaType == null) { mediaType = uc.getContentType(); } metaResource.setMediaType(mediaType); metaResource.setDescription(metaResource.getDescription()); metaResource.setContentStream(in); put(purePath, metaResource); } catch (IOException e) { String msg = "Could not read from the given URL: " + sourceURL; throw new RepositoryException(msg, e); } return purePath; }
From source file:org.wso2.carbon.registry.core.jdbc.Repository.java
/** * Creates a resource with the content imported from the source URL and meta data extracted from * the given meta data resource instance. Then the created resource is put to the registry using * the Repository.put() method./*from w ww . j av a 2s. c o m*/ * * @param path Path to put the resource * @param sourceURL URL to import resource content * @param metaResource Meta data for the new resource is extracted from this meta data resource * * @return Actual path where the new resource is stored in the registry * @throws RegistryException for all exceptional scenarios */ public String importResource(String path, String sourceURL, Resource metaResource) throws RegistryException { String purePath = RegistryUtils.getPureResourcePath(path); URL url; try { if (sourceURL == null || sourceURL.toLowerCase().startsWith("file:")) { String msg = "The source URL must not be file in the server's local file system"; throw new RegistryException(msg); } url = new URL(sourceURL); } catch (MalformedURLException e) { String msg = "Given source URL is not valid."; throw new RegistryException(msg, e); } try { URLConnection uc = url.openConnection(); InputStream in = uc.getInputStream(); String mediaType = metaResource.getMediaType(); if (mediaType == null) { mediaType = uc.getContentType(); } metaResource.setMediaType(mediaType); metaResource.setDescription(metaResource.getDescription()); metaResource.setContentStream(in); put(purePath, metaResource); } catch (IOException e) { String msg = "Could not read from the given URL: " + sourceURL; throw new RegistryException(msg, e); } return purePath; }
From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java
private TransInfo query_Returnship_getData(String id) { TransInfo info = new TransInfo(); ReturnGoodsLog obj = this.returnGoodsLogService.getObjById(CommUtil.null2Long(id)); try {/*from w w w .ja v a 2 s. c o m*/ ExpressCompany ec = this.queryExpressCompany(obj.getReturn_express_info()); String query_url = "http://api.kuaidi100.com/api?id=" + this.configService.getSysConfig().getKuaidi_id() + "&com=" + (ec != null ? ec.getCompany_mark() : "") + "&nu=" + obj.getExpress_code() + "&show=0&muti=1&order=asc"; URL url = new URL(query_url); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet = null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return info; if (type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead, charSet); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } info = Json.fromJson(TransInfo.class, content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return info; }
From source file:com.iskyshop.manage.buyer.action.OrderBuyerAction.java
private TransInfo query_ship_getData(String id) { TransInfo info = null;/* w ww . j a va 2 s . co m*/ OrderForm obj = this.orderFormService.getObjById(CommUtil.null2Long(id)); if (obj != null && !CommUtil.null2String(obj.getShipCode()).equals("")) { info = new TransInfo(); try { ExpressCompany ec = this.queryExpressCompany(obj.getExpress_info()); String query_url = "http://api.kuaidi100.com/api?id=" + this.configService.getSysConfig().getKuaidi_id() + "&com=" + (ec != null ? ec.getCompany_mark() : "") + "&nu=" + obj.getShipCode() + "&show=0&muti=1&order=asc"; URL url = new URL(query_url); URLConnection con = url.openConnection(); con.setAllowUserInteraction(false); InputStream urlStream = url.openStream(); String type = con.guessContentTypeFromStream(urlStream); String charSet = null; if (type == null) type = con.getContentType(); if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0) return info; if (type.indexOf("charset=") > 0) charSet = type.substring(type.indexOf("charset=") + 8); byte b[] = new byte[10000]; int numRead = urlStream.read(b); String content = new String(b, 0, numRead, charSet); while (numRead != -1) { numRead = urlStream.read(b); if (numRead != -1) { // String newContent = new String(b, 0, numRead); String newContent = new String(b, 0, numRead, charSet); content += newContent; } } info = Json.fromJson(TransInfo.class, content); urlStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } return info; }