List of usage examples for java.net URLConnection getContentType
public String getContentType()
From source file:ddf.catalog.source.opensearch.SecureRemoteConnectionImpl.java
@Override public BinaryContent getData(String urlStr) throws IOException { URL url = new URL(urlStr); URLConnection conn = url.openConnection(); if (conn instanceof HttpsURLConnection) { try {//www . j a v a 2 s . c o m ((HttpsURLConnection) conn).setSSLSocketFactory(getSocketFactory()); } catch (Exception e) { throw new IOException("Error in creating SSL socket.", e); } } conn.connect(); MimeType mimeType = DEFAULT_MIMETYPE; try { mimeType = new MimeType(conn.getContentType()); } catch (MimeTypeParseException e) { LOGGER.debug("Error creating mime type with input [" + conn.getContentType() + "], defaulting to " + DEFAULT_MIMETYPE.toString()); } return new BinaryContentImpl(conn.getInputStream(), mimeType); }
From source file:org.inbio.neoportal.web.controller.ProxyController.java
@RequestMapping(value = "*", method = RequestMethod.GET) public @ResponseBody void getUrlContent(@RequestParam String url, HttpServletResponse response) throws IOException { try {/*w ww. j ava 2 s. co m*/ //debug Logger.getLogger(ProxyController.class.getName()).log(Level.INFO, "Inica carga url: {0}", url); //Inicia llamada al url URL urlObj = new URL(url); URLConnection conn = urlObj.openConnection(); // conn.setDoInput(true); // conn.setDoOutput(false); HttpURLConnection httpUrl = (HttpURLConnection) conn; // httpUrl.setDoInput(true); // httpUrl.setDoOutput(false); if (httpUrl.getResponseCode() == 200) { //obtiene la respuesta InputStream is = null; is = conn.getInputStream(); //modifica el content type response.setContentType(conn.getContentType()); IOUtils.copy(is, response.getOutputStream()); } else response.setStatus(httpUrl.getResponseCode()); //httpUrl. } catch (SocketException ex) { //problema para traer los datos Logger.getLogger(ProxyController.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(ProxyController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.servoy.extension.MarketPlaceExtensionProvider.java
private String[] ws_getVersions(String extensionId) { ArrayList<String> versions = new ArrayList<String>(); ByteArrayOutputStream bos = null; BufferedInputStream bis = null; try {/*from w w w .ja va 2 s.co m*/ URLConnection ws_connection = ws_getConnection(WS_ACTION_VERSIONS, "text/json", extensionId, null); //$NON-NLS-1$ bos = new ByteArrayOutputStream(); bis = new BufferedInputStream(ws_connection.getInputStream()); byte[] buffer = new byte[1024]; int len; while ((len = bis.read(buffer)) != -1) bos.write(buffer, 0, len); String encoding = getCharset(ws_connection.getContentType()); if (encoding == null) encoding = "UTF-8"; //$NON-NLS-1$ JSONArray jsonVersions = new JSONArray(new String(bos.toByteArray(), encoding)); for (int i = 0; i < jsonVersions.length(); i++) versions.add(jsonVersions.getString(i)); } catch (Exception ex) { String msg = "Cannot get extension versions from marketplace. Error is : " + ex.getMessage(); //$NON-NLS-1$ Debug.error(ex); messages.addError(msg); } finally { Utils.closeInputStream(bis); Utils.closeOutputStream(bos); } return versions.toArray(new String[versions.size()]); }
From source file:org.apache.synapse.registry.url.SimpleURLRegistry.java
public RegistryEntry getRegistryEntry(String key) { if (log.isDebugEnabled()) { log.debug("Perform RegistryEntry lookup for key : " + key); }//from www.java 2 s .c o m URL url = SynapseConfigUtils.getURLFromPath(root + key, properties.get(SynapseConstants.SYNAPSE_HOME) != null ? properties.get(SynapseConstants.SYNAPSE_HOME).toString() : ""); if (url == null) { return null; } URLConnection connection = SynapseConfigUtils.getURLConnection(url); if (connection == null) { if (log.isDebugEnabled()) { log.debug("Cannot create a URLConnection for given URL : " + url); } return null; } RegistryEntryImpl wre = new RegistryEntryImpl(); wre.setKey(key); wre.setName(url.getFile()); wre.setType(connection.getContentType()); wre.setDescription("Resource at : " + url.toString()); wre.setLastModified(connection.getLastModified()); wre.setVersion(connection.getLastModified()); if (connection.getExpiration() > 0) { wre.setCachableDuration(connection.getExpiration() - System.currentTimeMillis()); } else { wre.setCachableDuration(getCachableDuration()); } return wre; }
From source file:net.ontopia.topicmaps.utils.tmrap.RemoteTopicIndex.java
protected InputSource getInputSource(String method, String params, boolean compress) throws IOException { String baseuri = viewBaseuri == null ? editBaseuri : viewBaseuri; String url = baseuri + method + "?" + params; URLConnection conn = this.getConnection(url, 0); InputSource src = new InputSource(url); if (!compress) { src.setByteStream(conn.getInputStream()); String ctype = conn.getContentType(); if (ctype != null && ctype.startsWith("text/xml")) { int pos = ctype.indexOf("charset="); if (pos != -1) src.setEncoding(ctype.substring(pos + 8)); }/* w w w . j av a 2 s .c om*/ } else src.setByteStream(new java.util.zip.GZIPInputStream(conn.getInputStream())); return src; }
From source file:org.apache.cocoon.generation.LinkStatusGenerator.java
/** * Retrieve a list of links of a url/*from www .j av a 2 s . c om*/ * * @param url_link_string url for requesting links, it is assumed that * url_link_string queries the cocoon view links, ie of the form * <code>http://host/foo/bar?cocoon-view=links</code> * @param url_of_referrer base url of which links are requested, ie of the form * <code>http://host/foo/bar</code> * @return List of links from url_of_referrer, as result of requesting url * url_link_string */ protected List getLinksFromConnection(String url_link_string, URL url_of_referrer) { List url_links = null; BufferedReader br = null; try { URL url_link = new URL(url_link_string); URLConnection conn = url_link.openConnection(); String content_type = conn.getContentType(); if (content_type == null) { getLogger().warn("No content type available for " + String.valueOf(url_link_string)); // caller checks if null return url_links; } if (getLogger().isDebugEnabled()) { getLogger().debug("Content-type: " + content_type); } if (content_type.equals(linkContentType) || content_type.startsWith(linkContentType + ";")) { url_links = new ArrayList(); InputStream is = conn.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); // content is supposed to be a list of links, // relative to current URL String line; String referrer = url_of_referrer.toString(); while ((line = br.readLine()) != null) { URL new_url = new URL(url_link, line); boolean add_url = true; // don't add new_url twice if (add_url) { add_url &= !url_links.contains(new_url); } // don't add new_url if it has been crawled already if (add_url) { add_url &= !crawled.contains(new_url.toString()); } Link new_link = new Link(new_url, referrer); if (add_url) { add_url &= !linksToProcess.contains(new_link); } // don't add if is not matched by existing include definition if (add_url) { add_url &= isIncludedURL(new_url.toString()); } if (add_url) { if (getLogger().isDebugEnabled()) { getLogger().debug("Add URL: " + new_url.toString()); } url_links.add(new_link); } } // now we have a list of URL which should be examined } } catch (IOException ioe) { getLogger().warn("Problems get links of " + url_link_string, ioe); } finally { // explictly close the stream if (br != null) { try { br.close(); br = null; } catch (IOException ignored) { } } } return url_links; }
From source file:org.apache.cocoon.generation.LinkStatusGenerator.java
/** * Generate xml attributes of a url, calculate url for retrieving links * * @param url to process/*from www. j a v a 2s.co m*/ * @param referrer of the url * @return String url for retrieving links, or null if url is an excluded-url, * and not an included-url. */ protected String processURL(URL url, String referrer) throws SAXException { if (getLogger().isDebugEnabled()) { getLogger().debug("getLinks URL " + url); } String result = null; // don't try to investigate a url which has been crawled already if (crawled.contains(url.toString())) { return null; } // mark it as crawled crawled.add(url.toString()); attributes.clear(); attributes.addAttribute("", HREF_ATTR_NAME, HREF_ATTR_NAME, "CDATA", url.toString()); attributes.addAttribute("", REFERRER_ATTR_NAME, REFERRER_ATTR_NAME, "CDATA", referrer); // Output url, referrer, content-type, status, message for traversable url's HttpURLConnection h = null; try { URLConnection links_url_connection = url.openConnection(); h = (HttpURLConnection) links_url_connection; String content_type = links_url_connection.getContentType(); attributes.addAttribute("", CONTENT_ATTR_NAME, CONTENT_ATTR_NAME, "CDATA", content_type); attributes.addAttribute("", MESSAGE_ATTR_NAME, MESSAGE_ATTR_NAME, "CDATA", h.getResponseMessage()); attributes.addAttribute("", STATUS_ATTR_NAME, STATUS_ATTR_NAME, "CDATA", String.valueOf(h.getResponseCode())); } catch (IOException ioe) { attributes.addAttribute("", MESSAGE_ATTR_NAME, MESSAGE_ATTR_NAME, "CDATA", ioe.getMessage()); } finally { if (h != null) { h.disconnect(); } } // don't try to get links of a url which is excluded from crawling // try to get links of a url which is included for crawling if (!isExcludedURL(url.toString()) && isIncludedURL(url.toString())) { // add prefix and query to get data from the linkserializer. result = url.toExternalForm() + ((url.toExternalForm().indexOf("?") == -1) ? "?" : "&") + linkViewQuery; } super.contentHandler.startElement(URI, LINK_NODE_NAME, PREFIX + ':' + LINK_NODE_NAME, attributes); super.contentHandler.endElement(URI, LINK_NODE_NAME, PREFIX + ':' + LINK_NODE_NAME); return result; }
From source file:it.polito.tellmefirst.web.rest.clients.ClientEpub.java
private Boolean urlIsEpub(String url) { LOG.debug("[checkURL] - BEGIN"); Boolean isEpub = false;//from w ww.j a v a 2 s . c o m URL epubUrl; String type; try { epubUrl = new URL(url); URLConnection urlConnection = epubUrl.openConnection(); type = urlConnection.getContentType(); if (type.equals("application/epub+zip")) isEpub = true; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } LOG.debug("[checkURL] - END"); return isEpub; }
From source file:com.google.android.dialer.provider.DialerProvider.java
private String executeHttpRequest(Uri uri) throws IOException { String charset = null;/*from w w w . ja va 2 s.com*/ if (Log.isLoggable("DialerProvider", Log.VERBOSE)) { Log.v("DialerProvider", "executeHttpRequest(" + uri + ")"); } try { URLConnection conn = new URL(uri.toString()).openConnection(); conn.setRequestProperty("User-Agent", mUserAgent); InputStream inputStream = conn.getInputStream(); charset = getCharsetFromContentType(conn.getContentType()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buf = new byte[1000]; while (true) { int len = inputStream.read(buf); if (len <= 0) { break; } outputStream.write(buf, 0, len); } inputStream.close(); outputStream.flush(); return new String(outputStream.toByteArray(), charset); } catch (UnsupportedEncodingException e) { Log.w("DialerProvider", "Invalid charset: " + charset, e); } catch (IOException e) { // TODO: Didn't find anything that goes here in byte-code } // TODO: Is this appropriate? return null; }
From source file:org.y20k.transistor.helpers.MetadataHelper.java
private void shoutcastProxyReaderLoop(Socket proxy, URLConnection connection) throws IOException { connection.setConnectTimeout(5000);/*from w ww. j a v a2 s .com*/ connection.setReadTimeout(5000); connection.setRequestProperty("Icy-MetaData", "1"); connection.connect(); InputStream in = connection.getInputStream(); OutputStream out = proxy.getOutputStream(); out.write(("HTTP/1.0 200 OK\r\n" + "Pragma: no-cache\r\n" + "Content-Type: " + connection.getContentType() + "\r\n\r\n").getBytes(StandardCharsets.UTF_8)); byte buf[] = new byte[16384]; // one second of 128kbit stream int count = 0; int total = 0; int metadataSize = 0; final int metadataOffset = connection.getHeaderFieldInt("icy-metaint", 0); int bitRate = Math.max(connection.getHeaderFieldInt("icy-br", 128), 32); LogHelper.v(LOG_TAG, "createProxyConnection: connected, icy-metaint " + metadataOffset + " icy-br " + bitRate); while (true) { count = Math.min(in.available(), buf.length); if (count <= 0) { count = Math.min(bitRate * 64, buf.length); // buffer half-second of stream data } if (metadataOffset > 0) { count = Math.min(count, metadataOffset - total); } count = in.read(buf, 0, count); if (count == 0) { continue; } if (count < 0) { break; } out.write(buf, 0, count); total += count; if (metadataOffset > 0 && total >= metadataOffset) { // read metadata total = 0; count = in.read(); if (count < 0) { break; } count *= 16; metadataSize = count; if (metadataSize == 0) { continue; } // maximum metadata length is 4080 bytes total = 0; while (total < metadataSize) { count = in.read(buf, total, count); if (count < 0) { break; } if (count == 0) { continue; } total += count; count = metadataSize - total; } total = 0; String[] metadata = new String(buf, 0, metadataSize, StandardCharsets.UTF_8).split(";"); for (String s : metadata) { if (s.indexOf(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER) == 0 && s.length() >= TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length() + 1) { handleMetadataString( s.substring(TransistorKeys.SHOUTCAST_STREAM_TITLE_HEADER.length(), s.length() - 1)); // break; } } } } }