GithubHelp home page GithubHelp logo

ruhan1 / jhttpc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from commonjava/jhttpc

0.0 2.0 0.0 136 KB

httpclient wrapper that handles per-site SSL configuration, flexible storage of various site-related passwords, and connection handling

Java 100.00%

jhttpc's Introduction

#jHTTPc - A Httpclient Wrapper

jHTTPc is a wrapper around Apache HttpClient that simplifies use cases related to server-side embedding, such as PEM-oriented SSL configuration. Using jHTTPc, you don't have to work directly with KeyStores, and you can specify an independent SSL configuration for each host to which your server connects. You can use PEM files on the system, or even PEM-encoded strings stored by some other mechanism. This also makes it easier to support user-driven configuration of these hosts via a server UI (or even REST), since keystore management happens behind the scenes.

##Basics

It's very easy to start using:

SiteConfigBuilder siteBuilder = new SiteConfigBuilder( "test", "http://www.somesite.com" )
                  .withKeyCertPem( FileUtils.readFileToString( "/path/to/client.pem" ) )
                  .withServerCertPem( FileUtils.readFileToString( "/path/to/my-server.pem" ) );

SiteConfig site = siteBuilder.build();

MemoryPasswordManager passwords = new MemoryPasswordManager();
passwords.bind( "my K3y password!", site, PasswordType.KEY );

HttpFactory factory = new HttpFactory( passwords );

HttpClient client = factory.createClient( site );
[...]

##Proxies

If you need to use a proxy server (with authentication), you can add the following:

siteBuilder.withProxyHost( "some.proxy.host" ).withProxyPort( 8080 ).withProxyUser( "someuser" );

SiteConfig site = siteBuilder.build();

passwords.bind( "somepassword", siteConfig, PasswordType.PROXY );

##Basic Authentication

If you need good old basic authentication (in addition to client SSL?!), you can specify that as well:

siteBuilder.withUser( "someuser" );

SiteConfig site = siteBuilder.build();

passwords.bind( "somepassword", siteConfig, PasswordType.USER );

##Other Goodies

There are other configurations you can specify on a per-site basis as well:

siteBuilder.withMaxConnections( 20 )
           .withRequestTimeoutSeconds( 30 )
           .withTrustType( ServerTrustType.TRUST_SELF_SIGNED );

##Custom Authenticators

If you need some other authentication mechanism, you can implement that by extending ClientAuthenticator and passing in your own authenticator instance via the HttpFactory(ClientAuthenticator) constructor. For instance, a simple OAuth bearer token authenticator might look like this:

public class OAuth20BearerTokenAuthenticator
        extends ClientAuthenticator
{

    private static final String AUTHORIZATION_HEADER = "Authorization";

    private static final String BEARER_FORMAT = "Bearer %s";

    private final String token;

    public OAuth20BearerTokenAuthenticator( final String token )
    {
        this.token = token;
    }

    @Override
    public HttpClientBuilder decorateClientBuilder( final HttpClientBuilder builder )
            throws JHttpCException
    {
        final Header header = new BasicHeader( AUTHORIZATION_HEADER, String.format( BEARER_FORMAT, token ) );
        return builder.setDefaultHeaders( Collections.<Header> singleton( header ) );
    }

}

Then, simply construct the HttpFactory using the new authenticator:

HttpFactory factory = new HttpFactory( new OAuth20BearerTokenAuthenticator( token ) );

jhttpc's People

Contributors

jdcasey avatar ligangty avatar rnc avatar ruhan1 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.