bwLehrpool Masterserver
Manages authentication and sharing of virtual machines between participating institutions
HttpListener.java
Go to the documentation of this file.
1 package org.openslx.imagemaster.thrift.server;
2 
3 import java.io.IOException;
4 import java.util.Arrays;
5 import java.util.concurrent.TimeUnit;
6 
7 import org.apache.logging.log4j.LogManager;
8 import org.apache.logging.log4j.Logger;
9 import org.apache.thrift.protocol.TJSONProtocol;
10 import org.apache.thrift.protocol.TProtocol;
11 import org.apache.thrift.transport.TMemoryBuffer;
12 import org.openslx.bwlp.thrift.iface.MasterServer;
13 import org.openslx.util.CascadedThreadPoolExecutor;
14 import org.openslx.util.Util;
15 
16 import fi.iki.elonen.NanoHTTPD;
17 
18 public class HttpListener extends NanoHTTPD
19 {
20 
21  private static final Logger LOGGER = LogManager.getLogger( HttpListener.class );
22 
23  private final MasterServer.Processor<MasterServerHandler> processor = new MasterServer.Processor<MasterServerHandler>(
24  new MasterServerHandler() );
25 
26  public HttpListener( String hostname, int port ) throws IOException
27  {
28  super( hostname, port, new CascadedThreadPoolExecutor( 4, 32, 1, TimeUnit.MINUTES, 2, "HTTP" ) );
29  this.maxRequestSize = 1_000_000;
30  }
31 
32  @Override
33  public Response serve( IHTTPSession session )
34  {
35  Method method = session.getMethod();
36  if ( Method.OPTIONS.equals( method ) ) {
37  Response response = new Response( Response.Status.NO_CONTENT, "application/json", "" );
38  addCorsHeaders( response );
39  return response;
40  }
41  if ( !Method.PUT.equals( method ) && !Method.POST.equals( method ) )
42  return new Response( Response.Status.BAD_REQUEST, "text/plain; charset=UTF-8", "Method not supported" );
43 
44  try {
45  //Input
46  String str = session.getHeaders().get( "content-length" );
47  int len = 0;
48  if ( str != null ) {
49  len = Util.parseInt( str, 0 );
50  }
51  if ( len <= 0 ) {
52  len = session.getInputStream().available();
53  }
54  if ( len <= 0 )
55  return new Response( Response.Status.BAD_REQUEST, "text/plain; charset=UTF-8", "No Content-Length provided" );
56 
57  byte[] buffer = session.getInputStream().readNBytes( len );
58  TMemoryBuffer inbuffer = new TMemoryBuffer( buffer.length );
59  inbuffer.write( buffer );
60  TProtocol inprotocol = new TJSONProtocol( inbuffer );
61 
62  //Output
63  TMemoryBuffer outbuffer = new TMemoryBuffer( 900 );
64  TProtocol outprotocol = new TJSONProtocol( outbuffer );
65 
66  processor.process( inprotocol, outprotocol );
67 
68  buffer = Arrays.copyOf( outbuffer.getArray(), outbuffer.length() );
69 
70  Response response = new Response( Response.Status.OK, "application/json", buffer );
71  addCorsHeaders( response );
72  return response;
73  } catch ( Throwable t ) {
74  if ( !t.getMessage().contains( "Remote side has closed" ) ) {
75  LOGGER.warn( "Error handling HTTP thrift", t );
76  }
77  return new Response( Response.Status.INTERNAL_ERROR, "text/plain; charset=UTF-8", t.getMessage() );
78  }
79 
80  }
81 
82  @Override
83  public void serverStopped()
84  {
85  System.exit( 1 );
86  }
87 
88  private static void addCorsHeaders( Response response )
89  {
90  response.addHeader( "Allow", "OPTIONS, GET, HEAD, POST, PUT" );
91  response.addHeader( "Access-Control-Allow-Methods", "*" );
92  response.addHeader( "Access-Control-Allow-Origin", "*" );
93  response.addHeader( "Access-Control-Allow-Headers", "*, Content-Type" );
94  response.addHeader( "Access-Control-Max-Age", "86400" );
95  }
96 
97 }
final MasterServer.Processor< MasterServerHandler > processor
Some utilities to make our lives easier.
Definition: Util.java:18
static void addCorsHeaders(Response response)