/** * {@inheritDoc} */ public void start(BundleContext context) throws Exception { Dictionary<String, Object> props = new Hashtable<>(); props.put(Constants.SERVICE_PID, ConnectionFactoryImpl.FACTORY_PID); props.put("impl.type", "jdk"); context.registerService(new String[]{ ConnectionFactory.class.getName(), ManagedServiceFactory.class.getName() }, new ConnectionFactoryImpl(), props); }
/** * Applies basic authentication to the given connection, if it is a {@link HttpURLConnection}. * * @param conn * the connection to apply basic authentication to; * @param values * the credentials to apply. */ private void applyBasicAuthentication(URLConnection conn, Object[] values) { if (conn instanceof HttpURLConnection) { conn.setRequestProperty(HTTP_HEADER_AUTHORIZATION, getBasicAuthCredentials(values)); } }
/** * {@inheritDoc} */ public URLConnection createConnection(URL url) throws IOException { if (url == null) { throw new IllegalArgumentException("URL cannot be null!"); } URLConnection conn = url.openConnection(); UrlCredentials creds = getCredentials(url); if (creds != null) { supplyCredentials(conn, creds); } return conn; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#getBasicAuthCredentials(UrlCredentials)}. */ @Test() public void testGetBasicAuthCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; String header = new ConnectionFactoryImpl().getBasicAuthCredentials(credentials.getCredentials()); assert header != null : "Expected valid HTTP header to be returned!"; assert header.equals(header.trim()) : "Expected HTTP header not to contain any leading/trailing whitespace!"; assert "Basic Zm9vOmJhcg==".equals(header) : "Expected HTTP header to be constant!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#deleted(java.lang.String)}. */ @Test() public void testDeleted() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; connFactory.deleted("pid1"); credentials = connFactory.getCredentials(TEST_URL); assert credentials == null : "Expected no credentials to be found!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String, java.util.Dictionary)}. */ @Test() public void testUpdatedInsertsCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); UrlCredentials credentials = connFactory.getCredentials(TEST_URL); assert credentials == null : "Expected no credentials to be found!"; Dictionary<String, ?> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); credentials = connFactory.getCredentials(TEST_URL); assert credentials != null : "Expected valid credentials to be found!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCreateConnectionNullUrlFail() throws Exception { new ConnectionFactoryImpl().createConnection(null); }
/** * Supplies the actual credentials to the given {@link URLConnection}. * * @param conn * the connection to supply the credentials to, cannot be <code>null</code>; * @param urlCreds * the URL credentials to supply, cannot be <code>null</code>. * @throws IOException * in case of I/O problems. */ private void supplyCredentials(URLConnection conn, UrlCredentials urlCreds) throws IOException { final AuthType type = urlCreds.getType(); final Object[] creds = urlCreds.getCredentials(); if (AuthType.BASIC.equals(type)) { applyBasicAuthentication(conn, creds); } else if (AuthType.CLIENT_CERT.equals(type)) { applyClientCertificate(conn, creds); } else if (!AuthType.NONE.equals(type)) { throw new IllegalArgumentException("Unknown authentication type: " + type); } } }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#updated(java.lang.String, java.util.Dictionary)}. */ @Test() public void testUpdatedUpdatesCredentialsOk() throws Exception { ConnectionFactoryImpl connFactory = new ConnectionFactoryImpl(); Dictionary<String, Object> props = createBasicAuthConfig(TEST_URL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials1 = connFactory.getCredentials(TEST_URL); assert credentials1 != null : "Expected valid credentials to be found!"; URL newURL = new URL("http://localhost:8181/test/"); props.put(UrlCredentialsFactory.KEY_AUTH_BASE_URL, newURL.toExternalForm()); connFactory.updated("pid1", props); UrlCredentials credentials2 = connFactory.getCredentials(TEST_URL); assert credentials2 == null : "Expected no credentials to be found!"; credentials2 = connFactory.getCredentials(newURL); assert credentials2 != null : "Expected valid credentials to be found!"; assert credentials1 != credentials2 && !credentials1.equals(credentials2) : "Expected not the same credentials to be returned!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL, org.osgi.service.useradmin.User)}. */ @Test() public void testCreateConnectionOk() throws Exception { URLConnection conn = new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo")); assert conn != null : "Expected valid connection to be created!"; }
/** * Test method for {@link org.apache.ace.connectionfactory.impl.ConnectionFactoryImpl#createConnection(java.net.URL, org.osgi.service.useradmin.User)}. */ @Test(expectedExceptions = IllegalArgumentException.class) public void testCreateConnectionNullUserFail() throws Exception { new ConnectionFactoryImpl().createConnection(new URL("file:///tmp/foo"), null); }
/** * {@inheritDoc} */ public URLConnection createConnection(URL url, User user) throws IOException { if (url == null) { throw new IllegalArgumentException("URL cannot be null!"); } if (user == null) { throw new IllegalArgumentException("User cannot be null!"); } URLConnection conn = url.openConnection(); UrlCredentials creds = getCredentials(url); if (creds != null) { // TODO apply user! supplyCredentials(conn, creds); } return conn; }