public HTTPProviderFactory(Class<CredentialsProvider> pclass) throws HTTPException { this.providerclass = pclass; if(pclass != null) try { this.constructor = pclass.getConstructor(AuthScope.class); } catch (NoSuchMethodException | SecurityException e) { throw new HTTPException("HTTPProviderFactory: no proper constructor available", e); } }
/** * Sets the HTTP authentication. * * @param url the url * @param username the username * @param password the password */ public static void setHTTPAuthentication(String url, String username, String password) { URL urlobj=null; try { urlobj = new URL(url); AuthScope authscope = new AuthScope(urlobj.getHost(), urlobj.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); ucar.httpservices.HTTPSession.setGlobalCredentials(credentials,authscope); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (HTTPException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
/** * Test that a large number of open/close does not lose connections; * check for null response. * This test uses an implicit HTTPSession. */ @Test public void testClosing2() throws HTTPException { // Set max # of connections HTTPSession.setGlobalMaxConnections(201); for(int i = 0; i < 200; i++) { HTTPMethod m = HTTPFactory.Get(CLOSEFILE); HttpResponse res = null; try { res = m.executeRaw(); } catch (HTTPException e) { if(e.getCause() instanceof ConnectionPoolTimeoutException) { System.err.println("TestMisc: timeout: " + i); } else throw e; } Assert.assertFalse("Null response", res == null); m.close(); } } }
@Test public void testFailBadUser() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("baadss", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
public void setRequestHeader(Header h) throws HTTPException { try { headers.add(h); } catch (Exception e) { throw new HTTPException("cause", e); } }
@Test public void testSuccess() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tds", "secret666")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertEquals(200, statusCode); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
private void acquirereadaccess() throws HTTPException { nreaders++; while(nwriters > 0) { try { wait(); } catch (InterruptedException e) { if(stop) throw new HTTPException("interrupted"); } } }
@Test public void testFailBadPassword() { String endpoint = TestOnLocalServer.withHttpPath(path); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { session.setCredentials(new UsernamePasswordCredentials("tiggeUser", "changeme")); HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); //if (statusCode != HttpStatus.SC_UNAUTHORIZED && statusCode != HttpStatus.SC_FORBIDDEN) // assert false; Assert.assertTrue(statusCode == HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
public void setMethodHeaders(List<Header> headers) throws HTTPException { try { for(Header h : headers) { this.headers.add(h); } } catch (Exception e) { throw new HTTPException(e); } }
@Test public void testFailNoAuth() { String endpoint = TestOnLocalServer.withHttpPath("/dodsC/testRestrictedDataset/testData2.nc.dds"); logger.info(String.format("testRestriction req = '%s'", endpoint)); try (HTTPSession session = HTTPFactory.newSession(endpoint)) { HTTPMethod method = HTTPFactory.Get(session); int statusCode = method.execute(); Assert.assertTrue("Expected HttpStatus.SC_UNAUTHORIZED|HttpStatus.SC_FORBIDDEN", statusCode== HttpStatus.SC_UNAUTHORIZED || statusCode == HttpStatus.SC_FORBIDDEN); } catch (ucar.httpservices.HTTPException e) { Assert.fail(e.getMessage()); } catch (Exception e) { e.printStackTrace(); Assert.fail(e.getMessage()); } } }
static URI decompose(String suri) throws HTTPException { try { URI uri = new URI(suri); return uri; } catch (URISyntaxException use) { throw new HTTPException("HTTPAuthScope: illegal url: " + suri); } }
public CredentialsProvider getProvider(AuthScope scope) throws HTTPException { if(this.providerclass == null) return null; else try { CredentialsProvider cp = this.constructor.newInstance(scope); return cp; } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new HTTPException("HTTPProvider Factory failure", e); } } }
private void acquirewriteaccess() throws HTTPException { nwriters++; while(nwriters > 1) { try { wait(); } catch (InterruptedException e) { if(stop) throw new HTTPException("interrupted"); } } while(nreaders > 0) { try { wait(); } catch (InterruptedException e) { if(stop) throw new HTTPException("interrupted"); } } }
@Deprecated protected HTTPMethod setRequestHeader(Header h) throws HTTPException { try { this.headers.put(h.getName(), h.getValue()); } catch (Exception e) { throw new HTTPException("cause", e); } return this; }
@Deprecated public HTTPMethod setMethodHeaders(List<Header> headers) throws HTTPException { try { for(Header h : headers) { this.headers.put(h.getName(), h.getValue()); } } catch (Exception e) { throw new HTTPException(e); } return this; }
static protected List<Entry> // public to allow testing getDeserializedEntries(ObjectInputStream ois) throws HTTPException { try { List<Entry> entries = new ArrayList<Entry>(); int count = ois.readInt(); for(int i = 0; i < count; i++) { Entry e = (Entry) ois.readObject(); entries.add(e); } return entries; } catch (Exception e) { throw new HTTPException(e); } }
/** * Create a request, add headers, and content, * then send to HTTPSession to do the bulk of the work. * * @return statuscode */ public int execute() throws HTTPException { HttpResponse res = executeRaw(); if(res != null) return res.getStatusLine().getStatusCode(); else throw new HTTPException("HTTPMethod.execute: null response"); }
static KeyStore buildstore(String path, String password, String prefix) throws HTTPException { KeyStore store = null; try { if(path != null && password != null) { File storefile = new File(path); if(!storefile.canRead()) throw new HTTPException("Cannot read specified " + prefix + "store:" + storefile.getAbsolutePath()); store = KeyStore.getInstance("JKS"); InputStream is = null; try { is = new FileInputStream(storefile); store.load(is, password.toCharArray()); } finally { if(is != null) is.close(); } } } catch (Exception e) { throw new HTTPException(e); } return store; }
/** * Common method creation code so we can isolate mocking * * @param session * @return * @throws HTTPException */ static protected HTTPMethod makemethod(HTTPSession.Methods m, HTTPSession session, String url) throws HTTPException { HTTPMethod meth = null; if(MOCKMETHODCLASS == null) { // do the normal case meth = new HTTPMethod(m, session, url); } else {//(MOCKMETHODCLASS != null) java.lang.Class methodcl = MOCKMETHODCLASS; Constructor<HTTPMethod> cons = null; try { cons = methodcl.getConstructor(HTTPSession.Methods.class, HTTPSession.class, String.class); } catch (Exception e) { throw new HTTPException("HTTPFactory: no proper HTTPMethod constructor available", e); } try { meth = cons.newInstance(m, session, url); } catch (Exception e) { throw new HTTPException("HTTPFactory: HTTPMethod constructor failed", e); } } return meth; }