List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:com.alfaariss.oa.authentication.remote.saml2.idp.storage.config.IDPConfigStorage.java
/** * @see com.alfaariss.oa.engine.idp.storage.configuration.AbstractConfigurationStorage#createIDP(com.alfaariss.oa.api.configuration.IConfigurationManager, org.w3c.dom.Element) *//*from w w w. j a va 2 s . c om*/ @Override protected IIDP createIDP(IConfigurationManager configManager, Element config) throws OAException { SAML2IDP saml2IDP = null; try { String sID = configManager.getParam(config, "id"); if (sID == null) { _oLogger.error("No 'id' item found in 'organization' section in configuration"); throw new OAException(SystemErrors.ERROR_CONFIG_READ); } byte[] baSourceID = generateSHA1(sID); String sFriendlyName = configManager.getParam(config, "friendlyname"); if (sFriendlyName == null) { _oLogger.error("No 'friendlyname' item found in 'organization' section in configuration"); throw new OAException(SystemErrors.ERROR_CONFIG_READ); } String sDateLastModified = configManager.getParam(config, "lastmodified"); Date dLastModified = null; if (sDateLastModified != null) { // Convert to java.util.Date try { DateTime dt = ISODateTimeFormat.dateTimeNoMillis().parseDateTime(sDateLastModified); dLastModified = dt.toDate(); } catch (IllegalArgumentException iae) { _oLogger.info( "Invalid 'lastmodified' timestamp provided: " + sDateLastModified + "; ignoring."); dLastModified = null; } } String sMetadataURL = null; int iMetadataURLTimeout = -1; String sMetadataFile = null; Element eMetadata = configManager.getSection(config, "metadata"); if (eMetadata == null) { _oLogger.warn( "No optional 'metadata' section found in configuration for organization with id: " + sID); } else { Element eHttp = configManager.getSection(eMetadata, "http"); if (eHttp == null) { _oLogger.warn( "No optional 'http' section in 'metadata' section found in configuration for organization with id: " + sID); } else { sMetadataURL = configManager.getParam(eHttp, "url"); if (sMetadataURL == null) { _oLogger.error( "No 'url' item in 'http' section found in configuration for organization with id: " + sID); throw new OAException(SystemErrors.ERROR_CONFIG_READ); } URL urlTarget = null; try { urlTarget = new URL(sMetadataURL); } catch (MalformedURLException e) { _oLogger.error( "Invalid 'url' item in 'http' section found in configuration: " + sMetadataURL, e); throw new OAException(SystemErrors.ERROR_INIT); } StringBuffer sbInfo = new StringBuffer("Organization '"); sbInfo.append(sID); sbInfo.append("' uses metadata from url: "); sbInfo.append(sMetadataURL); _oLogger.info(sbInfo.toString()); try { URLConnection urlConnection = urlTarget.openConnection(); urlConnection.setConnectTimeout(3000); urlConnection.setReadTimeout(3000); urlConnection.connect(); } catch (IOException e) { _oLogger.warn("Could not connect to 'url' item in 'http' section found in configuration: " + sMetadataURL, e); } String sTimeout = configManager.getParam(eHttp, "timeout"); if (sTimeout != null) { try { iMetadataURLTimeout = Integer.parseInt(sTimeout); } catch (NumberFormatException e) { _oLogger.error( "Invalid 'timeout' item in 'http' section found in configuration (must be a number): " + sTimeout, e); throw new OAException(SystemErrors.ERROR_INIT); } if (iMetadataURLTimeout < 0) { _oLogger.error( "Invalid 'timeout' item in 'http' section found in configuration: " + sTimeout); throw new OAException(SystemErrors.ERROR_INIT); } } } sMetadataFile = configManager.getParam(eMetadata, "file"); if (sMetadataFile == null) { _oLogger.warn( "No optional 'file' item in 'metadata' section found in configuration for organization with id: " + sID); } else { // Translate the path sMetadataFile = PathTranslator.getInstance().map(sMetadataFile); File fMetadata = new File(sMetadataFile); if (!fMetadata.exists()) { _oLogger.error("Configured metadata 'file' doesn't exist: " + sMetadataFile); throw new OAException(SystemErrors.ERROR_INIT); } StringBuffer sbInfo = new StringBuffer("Organization '"); sbInfo.append(sID); sbInfo.append("' uses metadata in file: "); sbInfo.append(sMetadataFile); _oLogger.info(sbInfo.toString()); } } Boolean boolACSIndex = new Boolean(true); String sACSIndex = configManager.getParam(config, "acs_index"); if (sACSIndex != null) { if (sACSIndex.equalsIgnoreCase("FALSE")) boolACSIndex = new Boolean(false); else if (!sACSIndex.equalsIgnoreCase("TRUE")) { _oLogger.error("Invalid 'acs_index' item value found in configuration: " + sACSIndex); throw new OAException(SystemErrors.ERROR_INIT); } } Boolean boolScoping = new Boolean(true); String sScoping = configManager.getParam(config, "scoping"); if (sScoping != null) { if (sScoping.equalsIgnoreCase("FALSE")) boolScoping = new Boolean(false); else if (!sScoping.equalsIgnoreCase("TRUE")) { _oLogger.error("Invalid 'scoping' item value found in configuration: " + sScoping); throw new OAException(SystemErrors.ERROR_INIT); } } Boolean boolNameIDPolicy = new Boolean(true); String sNameIDFormat = null; Boolean boolAllowCreate = null; Element eNameIDPolicy = configManager.getSection(config, "nameidpolicy"); if (eNameIDPolicy != null) { String sNameIDPolicyEnabled = configManager.getParam(eNameIDPolicy, "enabled"); if (sNameIDPolicyEnabled != null) { if (sNameIDPolicyEnabled.equalsIgnoreCase("FALSE")) boolNameIDPolicy = new Boolean(false); else if (!sNameIDPolicyEnabled.equalsIgnoreCase("TRUE")) { _oLogger.error( "Invalid 'enabled' item value in 'nameidpolicy' section found in configuration: " + sNameIDPolicyEnabled); throw new OAException(SystemErrors.ERROR_INIT); } } if (boolNameIDPolicy) { String sAllowCreate = configManager.getParam(eNameIDPolicy, "allow_create"); if (sAllowCreate != null) { if (sAllowCreate.equalsIgnoreCase("TRUE")) boolAllowCreate = new Boolean(true); else if (sAllowCreate.equalsIgnoreCase("FALSE")) boolAllowCreate = new Boolean(false); else { _oLogger.error( "Invalid 'allow_create' item value found in configuration: " + sAllowCreate); throw new OAException(SystemErrors.ERROR_INIT); } } sNameIDFormat = configManager.getParam(eNameIDPolicy, "nameidformat"); } } Boolean boolAvoidSubjectConfirmation = new Boolean(false); // default: don't avoid String sAvoidSC = configManager.getParam(config, "avoid_subjectconfirmation"); if (sAvoidSC != null) { if (sAvoidSC.equalsIgnoreCase("TRUE")) boolAvoidSubjectConfirmation = new Boolean(true); else if (!sAvoidSC.equalsIgnoreCase("FALSE")) { _oLogger.error( "Invalid 'avoid_subjectconfirmation' item value found in configuration: " + sAvoidSC); throw new OAException(SystemErrors.ERROR_INIT); } } Boolean boolDisableSSOForIDP = new Boolean(false); // default: don't disable String sDisableSSO = configManager.getParam(config, "disable_sso"); if (sDisableSSO != null) { if (sDisableSSO.equalsIgnoreCase("TRUE")) boolDisableSSOForIDP = new Boolean(true); else if (!sDisableSSO.equalsIgnoreCase("FALSE")) { _oLogger.error("Invalid 'disable_sso' item value found in configuration: " + sDisableSSO); throw new OAException(SystemErrors.ERROR_INIT); } } saml2IDP = new SAML2IDP(sID, baSourceID, sFriendlyName, sMetadataFile, sMetadataURL, iMetadataURLTimeout, boolACSIndex, boolAllowCreate, boolScoping, boolNameIDPolicy, sNameIDFormat, boolAvoidSubjectConfirmation, boolDisableSSOForIDP, dLastModified, _sMPMId); } catch (OAException e) { throw e; } catch (Exception e) { _oLogger.fatal("Internal error while reading organization configuration", e); throw new OAException(SystemErrors.ERROR_INTERNAL); } return saml2IDP; }
From source file:io.s4.latin.adapter.TwitterFeedListener.java
public void connectAndRead() throws Exception { URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.setConnectTimeout(10000); connection.setReadTimeout(10000);//from ww w. j av a 2s . co m String userPassword = userid + ":" + password; System.out.println("connect to " + connection.getURL().toString() + " ..."); String encoded = EncodingUtil.getAsciiString(Base64.encodeBase64(EncodingUtil.getAsciiBytes(userPassword))); connection.setRequestProperty("Authorization", "Basic " + encoded); connection.setRequestProperty("Accept-Charset", "utf-8,ISO-8859-1"); connection.connect(); System.out.println("Connect OK!"); System.out.println("Reading TwitterFeed ...."); Long startTime = new Date().getTime(); InputStream is = connection.getInputStream(); Charset utf8 = Charset.forName("UTF-8"); InputStreamReader isr = new InputStreamReader(is, utf8); BufferedReader br = new BufferedReader(isr); String inputLine = null; while ((inputLine = br.readLine()) != null) { if (inputLine.trim().length() == 0) { blankCount++; continue; } messageCount++; messageQueue.add(inputLine); if (messageCount % 500 == 0) { Long currentTime = new Date().getTime(); System.out.println("Lines processed: " + messageCount + "\t ( " + blankCount + " empty lines ) in " + (currentTime - startTime) / 1000 + " seconds. Reading " + messageCount / ((currentTime - startTime) / 1000) + " rows/second"); } } }
From source file:fr.univlr.cri.planning.factory.HugICalendarFactory.java
/** * Lecture d'un fichier icalendar via l'API de la librairie ical4j. Les * occupations contenues dans la fenetre d'interrogation [dateDebut, dateFin] * sont retournees.// w w w . j a v a 2s.c om * * @param pathICalendar * : le chemin du fichier ICS * @param dateDebut * : date de debut de la periode d'interrogation * @param dateFin * : date de fin de la periode d'interrogation * @return * @throws CalendarNotFoundException */ private CalendarObject newCalendarObjectFromICalendarFileICal4J(String pathICalendar, NSTimestamp dateDebut, NSTimestamp dateFin) throws CalendarNotFoundException { CalendarObject oCal = null; InputStream in = null; try { // fin = new FileInputStream(pathICalendar); URL url = new URL(pathICalendar); URLConnection con = url.openConnection(); con.connect(); in = con.getInputStream(); } catch (FileNotFoundException e) { throw new CalendarNotFoundException("Calendar " + pathICalendar + " non trouv ou acc?s refus."); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * CalendarBuilder calendarBuilder = new CalendarBuilder(); * * try { long l1 = System.currentTimeMillis(); Calendar calendar = * calendarBuilder.build(in); oCal = new CalendarObject(pathICalendar); l1 = * System.currentTimeMillis() - l1; CktlLog.log(">> parsing : " + l1 + * " ms ("+pathICalendar+")"); * * // on passe au evenements for (Iterator i = * calendar.getComponents().iterator(); i.hasNext();) { Component component * = (Component) i.next(); if (component.getName().equals(Component.VEVENT)) * { VEvent vEvent = (VEvent) component; oCal.addSPVEvent(new * SPVEvent(vEvent)); } } * * * * } catch (IOException e) { throw new * CalendarNotFoundException("Calendar "+ pathICalendar * +" erreur de lecture " + e.getMessage()); } catch (ParserException e) { * throw new CalendarNotFoundException("Calendar "+ pathICalendar * +" erreur de lecture " + e.getMessage()); } */ String string = null; try { long l1 = System.currentTimeMillis(); URL url = new URL(pathICalendar); URLConnection con = url.openConnection(); con.connect(); BufferedInputStream bin = new BufferedInputStream(con.getInputStream()); StringWriter out = new StringWriter(); int b; while ((b = bin.read()) != -1) out.write(b); out.flush(); out.close(); bin.close(); string = out.toString(); l1 = System.currentTimeMillis() - l1; System.out.println("converting calendar url to string : " + l1 + " ms (" + pathICalendar + ")"); } catch (IOException ie) { ie.printStackTrace(); } long l1 = System.currentTimeMillis(); StringReader sin = new StringReader(string); CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = null; try { calendar = builder.build(sin); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } l1 = System.currentTimeMillis() - l1; System.out.println("parse calendar string : " + l1 + " ms (" + pathICalendar + ")"); oCal = new CalendarObject(pathICalendar); // on passe au evenements for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) { Component component = (Component) i.next(); if (component.getName().equals(Component.VEVENT)) { VEvent vEvent = (VEvent) component; oCal.addSPVEvent(new SPVEvent(vEvent)); } } // on met en cache notre affaire if (oCal != null) { putCalendarInCache(pathICalendar, oCal); } return oCal; }
From source file:hudson.cli.CLI.java
/** * If the server advertises CLI endpoint, returns its location. * @deprecated Specific to {@link Mode#REMOTING}. *///ww w . j av a 2 s . c o m @Deprecated protected CliPort getCliTcpPort(URL url) throws IOException { if (url.getHost() == null || url.getHost().length() == 0) { throw new IOException("Invalid URL: " + url); } URLConnection head = url.openConnection(); try { head.connect(); } catch (IOException e) { throw (IOException) new IOException("Failed to connect to " + url).initCause(e); } String h = head.getHeaderField("X-Jenkins-CLI-Host"); if (h == null) h = head.getURL().getHost(); String p1 = head.getHeaderField("X-Jenkins-CLI-Port"); if (p1 == null) p1 = head.getHeaderField("X-Hudson-CLI-Port"); // backward compatibility String p2 = head.getHeaderField("X-Jenkins-CLI2-Port"); String identity = head.getHeaderField("X-Instance-Identity"); flushURLConnection(head); if (p1 == null && p2 == null) { verifyJenkinsConnection(head); throw new IOException("No X-Jenkins-CLI2-Port among " + head.getHeaderFields().keySet()); } if (p2 != null) return new CliPort(new InetSocketAddress(h, Integer.parseInt(p2)), identity, 2); else return new CliPort(new InetSocketAddress(h, Integer.parseInt(p1)), identity, 1); }
From source file:org.wymiwyg.wrhapi.test.BaseTests.java
/** * Test whether the getPort method returns the actual request port and not the one * included in the host-header/* w w w. ja v a2 s .c om*/ * @throws Exception */ @Test public void testPort() throws Exception { WebServer webServer = createServer().startNewWebServer(new Handler() { public void handle(Request request, Response response) throws HandlerException { assertEquals(serverBinding.getPort(), request.getPort()); } }, serverBinding); try { URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":" + serverBinding.getPort() + "/"); URLConnection connection = serverURL.openConnection(); connection.setRequestProperty("host", "foo:88"); connection.setRequestProperty("FOO", "bar"); connection.connect(); // for the handler to be invoked, something of the response has to // be asked //assertEquals(headerValue, connection.getHeaderField("Cookie")); connection.getContentLength(); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { webServer.stop(); } }
From source file:org.kite9.diagram.server.AbstractKite9Controller.java
/** * Retrieves user info from cookie/* w ww . j a va2 s . c om*/ */ public User getUser(HttpServletRequest req) { if (isLocal()) { return LOCAL_USER; } Cookie[] cookies = req.getCookies(); String wpCookieName = null; String wpCookieValue = null; if (cookies != null) { for (Cookie cookie : cookies) { if (cookie.getName().startsWith("wordpress_logged_in")) { wpCookieName = cookie.getName(); wpCookieValue = cookie.getValue(); } } } final String ip = req.getRemoteAddr(); final String host = req.getRemoteHost(); System.out.println("Session : " + wpCookieName + " " + wpCookieValue); if (wpCookieName == null) { return NO_USER; } try { URL u = new URL(URL_ROOT + "/kite9_user_info"); URLConnection conn = u.openConnection(); conn.setRequestProperty("Cookie", wpCookieName + "=" + wpCookieValue); conn.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = br.readLine(); br.close(); if (line.contains("<none>")) { return NO_USER; } else { String parts[] = line.split(","); int id = Integer.parseInt(parts[1]); return new User(id, parts[0], false, ip, host); } } catch (IOException e) { throw new Kite9ProcessingException("Couldn't handle user log-in", e); } }
From source file:org.wymiwyg.wrhapi.test.BaseTests.java
/** * set a "cookie" request header and expects the same value in the "cookie" * response header/*from w w w .j av a2 s . co m*/ * * @throws Exception */ @Test public void testMultipleRequestHeader() throws Exception { final String headerValue = "bla blah"; WebServer webServer = createServer().startNewWebServer(new Handler() { public void handle(Request request, Response response) throws HandlerException { log.info("handling testMultipleRequestHeader"); String receivedHeaderValue = request.getHeaderValues(HeaderName.COOKIE)[0]; response.setHeader(HeaderName.COOKIE, receivedHeaderValue); assertEquals(headerValue, receivedHeaderValue); } }, serverBinding); try { URL serverURL = new URL("http://" + serverBinding.getInetAddress().getHostAddress() + ":" + serverBinding.getPort() + "/"); URLConnection connection = serverURL.openConnection(); connection.setRequestProperty("CoOkie", headerValue); connection.setRequestProperty("FOO", "bar"); connection.connect(); // for the handler to be invoked, something of the response has to // be asked assertEquals(headerValue, connection.getHeaderField("Cookie")); connection.getContentLength(); } catch (MalformedURLException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } finally { webServer.stop(); } }
From source file:org.apache.cocoon.components.crawler.SimpleCocoonCrawlerImpl.java
/** * Compute list of links from the url./*ww w.j av a2 s . c o m*/ * <p> * Check for include, exclude pattern, content-type, and if url * has been craweled already. * </p> * * @param url Crawl this URL * @return List of URLs, which are links from url, asserting the conditions. * @since */ private List getLinks(URL url) { ArrayList url_links = null; String sURL = url.toString(); if (!isIncludedURL(sURL) || isExcludedURL(sURL)) { return null; } // don't try to get links for url which has been crawled already if (crawled.contains(sURL)) { return null; } // mark it as crawled crawled.add(sURL); // get links of url if (getLogger().isDebugEnabled()) { getLogger().debug("Getting links of URL " + sURL); } BufferedReader br = null; try { sURL = url.getFile(); URL links = new URL(url, sURL + ((sURL.indexOf("?") == -1) ? "?" : "&") + linkViewQuery); URLConnection links_url_connection = links.openConnection(); links_url_connection.setRequestProperty("Accept", accept); links_url_connection.setRequestProperty("User-Agent", userAgent); links_url_connection.connect(); InputStream is = links_url_connection.getInputStream(); br = new BufferedReader(new InputStreamReader(is)); String contentType = links_url_connection.getContentType(); if (contentType == null) { if (getLogger().isDebugEnabled()) { getLogger().debug("Ignoring " + sURL + " (no content type)"); } // there is a check on null in the calling method return null; } int index = contentType.indexOf(';'); if (index != -1) { contentType = contentType.substring(0, index); } if (getLogger().isDebugEnabled()) { getLogger().debug("Content-type: " + contentType); } if (contentType.equals(linkContentType)) { url_links = new ArrayList(); // content is supposed to be a list of links, // relative to current URL String line; while ((line = br.readLine()) != null) { final URL newUrl = new URL(url, line); final String sNewUrl = newUrl.toString(); boolean add_url = true; // don't add new_url twice if (add_url) { add_url &= !url_links.contains(sNewUrl); } // don't add new_url if it has been crawled already if (add_url) { add_url &= !crawled.contains(sNewUrl); } // don't add if is not matched by existing include definition if (add_url) { add_url &= isIncludedURL(sNewUrl); } // don't add if is matched by existing exclude definition if (add_url) { add_url &= !isExcludedURL(sNewUrl); } if (add_url) { if (getLogger().isDebugEnabled()) { getLogger().debug("Add URL: " + sNewUrl); } url_links.add(newUrl); } } // now we have a list of URL which should be examined } } catch (IOException ioe) { getLogger().warn("Problems get links of " + url, ioe); } finally { if (br != null) { try { br.close(); br = null; } catch (IOException ignored) { } } } return url_links; }
From source file:org.pentaho.osgi.platform.webjars.WebjarsURLConnection.java
@Override public InputStream getInputStream() throws IOException { try {/*from w w w.j a va 2 s .co m*/ final PipedOutputStream pipedOutputStream = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(pipedOutputStream); // making this here allows to fail with invalid URLs final URLConnection urlConnection = url.openConnection(); urlConnection.connect(); final InputStream originalInputStream = urlConnection.getInputStream(); transform_thread = EXECUTOR.submit( new WebjarsTransformer(url, originalInputStream, pipedOutputStream, this.minificationEnabled)); return pipedInputStream; } catch (Exception e) { logger.error(getURL().toString() + ": Error opening url"); throw new IOException("Error opening url", e); } }
From source file:org.geosdi.geoplatform.gui.server.service.impl.LayerService.java
@Override public boolean checkKmlUrl(String urlString) throws GeoPlatformException { try {/* w w w . j a v a2s . co m*/ URL url = new URL(urlString); URLConnection myURLConnection = url.openConnection(); myURLConnection.connect(); // String contentType = myURLConnection.getContentType(); // if (!contentType.contains("xml")) { // return true; // } } catch (IOException ex) { logger.info("Error on executing Check url: " + ex); throw new GeoPlatformException("Error on executing ParseURLServlet."); } return false; }