GithubHelp home page GithubHelp logo

java-osb's Introduction

Java Open Service Broker

Build Status Maven Central Javadocs License

The Java Open Service Broker is Java library for developing an Open Service Broker.

It supports all features of the OSBAPI v2.14 specifications, including asynchronous Service Bindings and fetching Service Instances and Service Bindings.

This project is an early state and has not been extensively tested. Please report issues!

Goals

  • Compliant with the Open Service Broker specification
  • Lightweight
  • Works with almost no external dependencies
  • Can be used for stand-alone and embedded Service Brokers

Download

Download the latest JAR or grab via Maven:

<dependency>
  <groupId>de.fmui.osb.broker</groupId>
  <artifactId>java-osb-lib</artifactId>
  <version>0.0.2</version>
</dependency>

Usage

Implementing a broker handler

The business logic of your Service Broker resides in an object that implements the OpenServiceBrokerHandler interface. It is recommended to extend the class AbstractOpenServiceBrokerHandler instead of implementing the the interface directly.

The interface contains a method for each operation defined in the OSBAPI specification. All methods follow the same pattern:

OperationResponse operation(OperationRequest request) throws OpenServiceBrokerException

  • The request object contains all input parameters and the request body.
  • There is builder for each response object (OperationResponse.builder()), setting the HTTP status code and the response body.
  • The request and response bodies and all objects defined in the OSBAPI specification are glorified Map<String, Object> objects, which allow setting and getting vendor extension fields.
  • To return an error response, throw one of the exceptions derived from OpenServiceBrokerException.

See the example project for code samples.

Authentication

Authentication can be handled inside or outside the handler. If it is handled outside, the authenticate() method should be empty. If it is handled inside, the credentials check should happen in this method. This method is called before each operation method called. If the credentials are incorrect, throw an UnauthorizedException.

public void authenticate(RequestCredentials credentials) throws OpenServiceBrokerException {

  if (!credentials.isBasicAuthentication()) {
    throw new UnauthorizedException();
  }

  BasicAuthCredentials basicAuth = (BasicAuthCredentials) credentials;
  String username = basicAuth.getUsername();
  String password = basicAuth.getPassword();

  if (!check(username, password)) {
    throw new UnauthorizedException();
  }
}

Creating a stand-alone broker

The easiest way to build a stand-alone broker is to extend the OpenServiceBrokerServlet class.

@WebServlet("/my-broker/*")
public class MyBrokerServlet extends OpenServiceBrokerServlet {
  @Override
  public void init(ServletConfig config) throws ServletException {
    setOpenServiceBrokerHandler(new MyOSBHandler());
  }
}

Creating an embedded broker

To embed a broker into an existing servlet, use an OpenServiceBroker object. The processRequest() method parses the request, calls the broker handler, and sends the response.

@WebServlet("/my-service/*")
public class MyServiceServlet extends HttpServlet {

  private OpenServiceBroker broker = new OpenServiceBroker();
  private OpenServiceBrokerHandler handler = new MyOSBHandler();

  @Override
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    if (request.getPathInfo().startsWith("/broker/")) {
      // http://<host>/<context>/my-service/broker/...
      broker.processRequest(request, response, handler);
      return;
    } 
    
    // ... other code here ...
  }
}

Logging errors

The library writes error messages to stderr. This behavior can be changed by passing an object that implements the ErrorLogHandler interface to the setErrorLogHandler() method.

Converting contexts

The OSBAPI specification defines a context object, which contains platform specfic data. The default implementation converts Cloud Foundry context objects and Kubernetes context objects into the respective Java objects.

This conversion can be changed and extended to other platform by providing a custom ContextHandler object to the setContextHandler() method.

Example Service Broker

The example project contains code examples for a synchronous and an asynchronous Service Broker.

java-osb's People

Contributors

fmui avatar

Watchers

 avatar  avatar

Forkers

kanthgithub

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.