Skip to content

BinaryIgor/Bright-Server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Test Coverage

Bright Server

Bright Server is easy to use, robust, reliable and flexible standalone java http server and lightweight web framework.

public final class SimpleApplication {

   public static void main(String[] args) throws Exception {
       RequestMethod get = new GetMethod();
   
       List<ConditionalRespondent> respondents = new ArrayList<>();
       ConditionalRespondent helloRespondent = new HttpRespondent("hello/{id:int}", get, new HelloRespondent());
       respondents.add(helloRespondent);

       List<ConditionalRequestFilter> filters = new ArrayList<>();
       ConditionalRequestFilter authorizationFilter = new HttpRequestFilter("*", new AnyRequestMethodRule(),
          new AuthorizationFilter());
       filters.add(authorizationFilter);

       Application application = new HttpApplication(new AllowAllCors(), respondents, filters);
       Connection connection = new RequestResponseConnection(new HttpOneProtocol(), application);
       Server server = new Server(8080, 5000, connection);
       server.start();
   }
}

That's all. Now you have fully working http server which handles get request nad authorize it as follows:

public final class AuthorizationFilter implements RequestFilter {

    private static final String SECRET_TOKEN = "token";
    private static final String AUTHORIZATION_HEADER = "Authorization";

   @Override
   public IntermediateResponse response(Request request) {
    	Response response;
    	if (request.hasHeader(AUTHORIZATION_HEADER)) {
    	    String token = request.header(AUTHORIZATION_HEADER);
    	    boolean valid = token.equals(SECRET_TOKEN);
    	    response = valid ? new ToForwardResponse() : new BlockedResponse(new ForbiddenResponse());
    	} else {
    	    response = new BlockedResponse(new ForbiddenResponse());
    	}
    	return response;
    }
}
public final class HelloRespondent implements Respondent {

    @Override
    public Response response(MatchedRequest request) {
        Response response;
	try {
	    int id = request.pathVariables().numberValue("id").intValue();
	    String message = "Hello number " + id;
	    response = new OkResponse(message);
	} catch (Exception e) {
	    response = new BadRequestResponse(e.getMessage());
	}
	return response;
    }
}

For more, refer to one page docs. Be sure to check out examples, which are always the best documentation. Project is under active development, so any feedback or opened issue is very welcome and appreciated. Because of that, they might become outdated from time to time.

Maven

<dependency>
  <groupId>com.iprogrammerr</groupId>
  <artifactId>bright-server</artifactId>
  <version>1.0.3</version>
</dependency>

Gradle

compile 'com.iprogrammerr:bright-server:1.0.3'

About

Simple, light weight, easy to use http server

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published