/** * Return the input stream to artifact. * * @return prepared input stream * * @throws IOException re-thrown * @see org.ops4j.net.URLUtils#prepareInputStream(java.net.URL,boolean) */ InputStream getInputStream() throws IOException { return URLUtils.prepareInputStream( m_artifactURL, !m_checkCertificate ); }
/** * Finds a free socket upon first calll and returns the same for every next call. * * @return a free port (from first call) * * @throws RuntimeException if no port has been found. (TODO change this to an apropriate checked exception) */ public int getPort() { if( m_found == -1 ) { m_found = findFree(); } return m_found; }
private int findFree() { for( int i = m_from; i <= m_to; i++ ) { if( isFree( i ) ) { return i; } } throw new RuntimeException( "No free port in range " + m_from + ":" + m_to ); }
/** * This will make sure a registry exists and is valid m_port. If its not available or does not work for some reason, * it will select another port. This should really not happen usually. But it can. * * @return this for fluent API. Or IllegalStateException if a port has not been detected successfully. */ public synchronized RMIRegistry selectGracefully() { // if( ( m_port = select( m_defaultPort ) ) == UNSELECTED ) { int alternativePort = new FreePort(m_altMin, m_altTo).getPort(); if ((m_port = select(alternativePort)) == UNSELECTED) { throw new IllegalStateException("No port found for RMI at all. Even though " + alternativePort + " should have worked. Thats.. not. good. at. all."); } printTakenStatus(); // } return this; }
/** * Prepare url for authentication and ssl if necessary and returns the input stream from the url. * * @param url url to prepare * @param acceptAnyCertificate true if the certicate check should be skipped * * @return input stream from url * * @throws IOException re-thrown */ public static InputStream prepareInputStream( final URL url, final boolean acceptAnyCertificate ) throws IOException { final URLConnection conn = url.openConnection(); prepareForAuthentication( conn ); prepareHttpHeaders( conn ); if( acceptAnyCertificate ) { prepareForSSL( conn ); } return conn.getInputStream(); }
/** * Prepares an url connection for authentication if necessary. * * @param connection the connection to be prepared * * @return the prepared conection */ public static URLConnection prepareForAuthentication( final URLConnection connection ) { NullArgumentException.validateNotNull( connection, "url connection cannot be null" ); if( connection.getURL().getUserInfo() != null ) { // Need to decode username/password because it may contain encoded characters (http://www.w3schools.com/tags/ref_urlencode.asp) // A common encoding is to provide a username as an email address like user%40domain.org String decodedUserInfo = decode( connection.getURL().getUserInfo() ); String base64Encoded = Base64Encoder.encode( decodedUserInfo ); // sun bug 6459815: Long passwords cause Basic Auth to fail with a java.net.Authenticator base64Encoded = base64Encoded.replaceAll( "\n", "" ); connection.setRequestProperty( "Authorization", "Basic " + base64Encoded ); } return connection; }
/** * Encodes a String into a base 64 String. The resulting encoding is chunked at 76 bytes. * <p> * * @param s String to encode. * * @return encoded string. */ public static String encode( String s ) { byte[] sBytes = s.getBytes(); sBytes = encode( sBytes ); s = new String( sBytes ); return s; }
/** * @return Function that returns {@code true} when there's a response from the URL; otherwise {@code false} */ public static Callable<Boolean> responseFrom(final URL url) { return () -> { HttpURLConnection conn = null; try { conn = (HttpURLConnection) url.openConnection(); if (conn instanceof HttpsURLConnection) { // relax host and certificate checks as we just want to see if it's up ((HttpsURLConnection) conn).setHostnameVerifier((hostname, session) -> true); URLUtils.prepareForSSL(conn); } conn.setRequestMethod("HEAD"); conn.connect(); return true; } finally { if (conn != null) { conn.disconnect(); } } }; }
public static void main( String[] args ) throws Exception { String s = "\nPGRpdiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94aHRtbCI+V2UncmUgcHJvcG9zaW5nIDxhIGhy\n" + "ZWY9Imh0dHA6Ly93d3cuZ29vZ2xlLmNvbS9jb3Jwb3JhdGUvc29mdHdhcmVfcHJpbmNpcGxlcy5odG1sIj5z\n" + "b21lIGd1aWRlbGluZXMgPC9hPnRvIGhlbHAgY3VyYiB0aGUgcHJvYmxlbSBvZiBJbnRlcm5ldCBzb2Z0d2Fy\n" + "ZSB0aGF0IGluc3RhbGxzIGl0c2VsZiB3aXRob3V0IHRlbGxpbmcgeW91LCBvciBiZWhhdmVzIGJhZGx5IG9u\n" + "Y2UgaXQgZ2V0cyBvbiB5b3VyIGNvbXB1dGVyLiBXZSd2ZSBiZWVuIGhlYXJpbmcgYSBsb3Qgb2YgY29tcGxh\n" + "aW50cyBhYm91dCB0aGlzIGxhdGVseSBhbmQgaXQgc2VlbXMgdG8gYmUgZ2V0dGluZyB3b3JzZS4gV2UgdGhp\n" + "bmsgaXQncyBpbXBvcnRhbnQgdGhhdCB5b3UgcmV0YWluIGNvbnRyb2wgb2YgeW91ciBjb21wdXRlciBhbmQg\n" + "dGhhdCB0aGVyZSBiZSBzb21lIGNsZWFyIHN0YW5kYXJkcyBpbiBvdXIgaW5kdXN0cnkuIExldCB1cyBrbm93\n" + "IGlmIHlvdSB0aGluayB0aGVzZSBndWlkZWxpbmVzIGFyZSB1c2VmdWwgb3IgaWYgeW91IGhhdmUgc3VnZ2Vz\n" + "dGlvbnMgdG8gaW1wcm92ZSB0aGVtLgo8YnIgLz4KPGJyIC8+Sm9uYXRoYW4gUm9zZW5iZXJnCjxiciAvPgo8\n" + "L2Rpdj4K\n"; System.out.println( decode( s ) ); } }
/** * Stores a URLConnection in association with a key. * * @param key The key that is associated to the URLConnection. * @param conn The URLConnection that should be stored in association with the key. */ public void put( Object key, URLConnection conn ) { synchronized( this ) // ensure no ConcurrentModificationException can occur. { Entry entry = new Entry( conn ); m_hardStore.put( key, entry ); if( m_thread == null ) { m_thread = new Thread( this, "ConnectionCache-cleaner" ); m_thread.setDaemon( true ); m_thread.start(); } } }
/** * {@inheritDoc} */ @Override public InputStream getDefinition( final Configuration configuration ) throws IOException { return URLUtils.prepareInputStream( new URL( configuration.getProperty( "felix.snapshot.definition.url" ) ), true ); }
/** * This will make sure a registry exists and is valid m_port. * If its not available or does not work for some reason, it will select another port. * This should really not happen usually. But it can. * * @return this for fluent API. Or IllegalStateException if a port has not been detected successfully. */ public synchronized RMIRegistry selectGracefully() { //if( ( m_port = select( m_defaultPort ) ) == UNSELECTED ) { int alternativePort = new FreePort( m_altMin, m_altTo ).getPort(); if( ( m_port = select( alternativePort ) ) == UNSELECTED ) { throw new IllegalStateException( "No port found for RMI at all. Even though " + alternativePort + " should have worked. Thats.. not. good. at. all." ); } printTakenStatus(); //} return this; }
/** * Encrypts and returns the encrypted string for the given message. * * @param message the pass phrase that is to be encrypted. * @return the encrypted string for the given message. */ static String encrypt(String message) { java.security.MessageDigest d =null; try { d = java.security.MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException e) { return null; } d.reset(); d.update(message.getBytes()); return new String(Base64Encoder.encode(d.digest())); }
/** * Decodes a base 64 String into a String. * <p> * * @param s String to decode. * * @return encoded string. * * @throws java.lang.IllegalArgumentException * thrown if the given byte array was not valid com.sun.syndication.io.impl.Base64 encoding. */ public static String decode( String s ) throws IllegalArgumentException { s = s.replaceAll( "\n", "" ); s = s.replaceAll( "\r", "" ); byte[] sBytes = s.getBytes(); sBytes = decode( sBytes ); s = new String( sBytes ); return s; }
/** * {@inheritDoc} */ @Override public InputStream getDefinition( final Configuration configuration ) throws IOException { return URLUtils.prepareInputStream( new URL( configuration.getProperty( "concierge.snapshot.definition.url" ) ), true ); }
/** * {@inheritDoc} */ @Override public InputStream getDefinition( final Configuration configuration ) throws IOException { return URLUtils.prepareInputStream( new URL( configuration.getProperty( "knopflerfish.snapshot.definition.url" ) ), true ); }
/** * {@inheritDoc} */ @Override public InputStream getDefinition( final Configuration configuration ) throws IOException { return URLUtils.prepareInputStream( new URL( configuration.getProperty( "equinox.snapshot.definition.url" ) ), true ); }
try is = URLUtils.prepareInputStream( bndFileURL, true ); m_wrappingProperties.load( is );
/** * Url must be a reference to an instructions file. * * @see AbstractConnection#getInstructions() */ protected Properties getInstructions() throws IOException { final Properties instructions = new Properties(); final URL instructionsFleUrl = getInstructionsFileURL(); instructions.load( URLUtils.prepareInputStream( instructionsFleUrl, !getConfiguration().getCertificateCheck() ) ); //the following line is for debugging purposes //instructions.store( System.out, null ); return instructions; }