Review Board 1.7.22


FLUME-2036. Make hostname optional for HTTPSource

Review Request #10984 - Created May 7, 2013 and submitted

Hari Shreedharan
FLUME-2036
Reviewers
Flume
flume-git
Made the hostname optional and added default of 0.0.0.0. Added docs and changed the host param to "bind" to match what we do in thrift and avro sources
Ran full tests
flume-ng-core/src/main/java/org/apache/flume/source/http/HTTPSource.java
Revision a96fc0d New Change
[20] 34 lines
[+20]
35
import javax.servlet.http.HttpServlet;
35
import javax.servlet.http.HttpServlet;
36
import javax.servlet.http.HttpServletRequest;
36
import javax.servlet.http.HttpServletRequest;
37
import javax.servlet.http.HttpServletResponse;
37
import javax.servlet.http.HttpServletResponse;
38
import java.io.IOException;
38
import java.io.IOException;
39
import java.util.ArrayList;
39
import java.util.ArrayList;

    
   
40
import java.util.Collections;
40
import java.util.List;
41
import java.util.List;
41
import java.util.Map;
42
import java.util.Map;
42

    
   
43

   
43
/**
44
/**
44
 * A source which accepts Flume Events by HTTP POST and GET. GET should be used
45
 * A source which accepts Flume Events by HTTP POST and GET. GET should be used
[+20] [20] 42 lines
[+20] [+] public class HTTPSource extends AbstractSource implements
87

    
   
88

   
88
  @Override
89
  @Override
89
  public void configure(Context context) {
90
  public void configure(Context context) {
90
    try {
91
    try {
91
      port = context.getInteger(HTTPSourceConfigurationConstants.CONFIG_PORT);
92
      port = context.getInteger(HTTPSourceConfigurationConstants.CONFIG_PORT);
92
      checkPort();
93
      host = context.getString(HTTPSourceConfigurationConstants.CONFIG_BIND,
93
      host = context.getString(HTTPSourceConfigurationConstants.CONFIG_HOST);
94
        HTTPSourceConfigurationConstants.DEFAULT_BIND);
94
      checkHost();
95
      checkHostAndPort();
95
      String handlerClassName = context.getString(
96
      String handlerClassName = context.getString(
96
              HTTPSourceConfigurationConstants.CONFIG_HANDLER,
97
              HTTPSourceConfigurationConstants.CONFIG_HANDLER,
97
              HTTPSourceConfigurationConstants.DEFAULT_HANDLER);
98
              HTTPSourceConfigurationConstants.DEFAULT_HANDLER).trim();
98
      @SuppressWarnings("unchecked")
99
      @SuppressWarnings("unchecked")
99
      Class<? extends HTTPSourceHandler> clazz =
100
      Class<? extends HTTPSourceHandler> clazz =
100
              (Class<? extends HTTPSourceHandler>)
101
              (Class<? extends HTTPSourceHandler>)
101
              Class.forName(handlerClassName);
102
              Class.forName(handlerClassName);
102
      handler = clazz.getDeclaredConstructor().newInstance();
103
      handler = clazz.getDeclaredConstructor().newInstance();
[+20] [20] 14 lines
[+20] public void configure(Context context) {
117
      LOG.error("Error configuring HTTPSource!", ex);
118
      LOG.error("Error configuring HTTPSource!", ex);
118
      Throwables.propagate(ex);
119
      Throwables.propagate(ex);
119
    }
120
    }
120
  }
121
  }
121

    
   
122

   
122
    private void checkHost() {
123
  private void checkHostAndPort() {
123
        Preconditions.checkNotNull(host, "HTTPSource requires a hostname to be"
124
    Preconditions.checkState(host != null && !host.isEmpty(),
124
                + "specified");
125
      "HTTPSource hostname specified is empty");

    
   
126
    Preconditions.checkNotNull(port, "HTTPSource requires a port number to be"

    
   
127
      + " specified");
125
    }
128
  }
126

    
   
129

   
127
    @Override
130
    @Override
128
  public void start() {
131
  public void start() {
129
    checkPort();

   
130
    Preconditions.checkState(srv == null,
132
    Preconditions.checkState(srv == null,
131
            "Running HTTP Server found in source: " + getName()
133
            "Running HTTP Server found in source: " + getName()
132
            + " before I started one."
134
            + " before I started one."
133
            + "Will not attempt to start.");
135
            + "Will not attempt to start.");
134
    srv = new Server();
136
    srv = new Server();
[+20] [20] 25 lines
[+20] [+] public void stop() {
160
    } catch (Exception ex) {
162
    } catch (Exception ex) {
161
      LOG.error("Error while stopping HTTPSource. Exception follows.", ex);
163
      LOG.error("Error while stopping HTTPSource. Exception follows.", ex);
162
    }
164
    }
163
  }
165
  }
164

    
   
166

   
165
  private void checkPort() {

   
166
    Preconditions.checkNotNull(port, "HTTPSource requires a port number to be"

   
167
            + "specified");

   
168
  }

   
169

    
   

   
170
  private class FlumeHTTPServlet extends HttpServlet {
167
  private class FlumeHTTPServlet extends HttpServlet {
171

    
   
168

   
172
    private static final long serialVersionUID = 4891924863218790344L;
169
    private static final long serialVersionUID = 4891924863218790344L;
173

    
   
170

   
174
    @Override
171
    @Override
175
    public void doPost(HttpServletRequest request, HttpServletResponse response)
172
    public void doPost(HttpServletRequest request, HttpServletResponse response)
176
            throws IOException {
173
            throws IOException {
177
      List<Event> events = new ArrayList<Event>(0); //create empty list
174
      List<Event> events = Collections.emptyList(); //create empty list
178
      try {
175
      try {
179
        events = handler.getEvents(request);
176
        events = handler.getEvents(request);
180
      } catch (HTTPBadRequestException ex) {
177
      } catch (HTTPBadRequestException ex) {
181
        LOG.warn("Received bad request from client. ", ex);
178
        LOG.warn("Received bad request from client. ", ex);
182
        response.sendError(HttpServletResponse.SC_BAD_REQUEST,
179
        response.sendError(HttpServletResponse.SC_BAD_REQUEST,
[+20] [20] 39 lines
flume-ng-core/src/main/java/org/apache/flume/source/http/HTTPSourceConfigurationConstants.java
Revision e7b3c7a New Change
 
flume-ng-doc/sphinx/FlumeUserGuide.rst
Revision 83e96f5 New Change
 
  1. flume-ng-core/src/main/java/org/apache/flume/source/http/HTTPSource.java: Loading...
  2. flume-ng-core/src/main/java/org/apache/flume/source/http/HTTPSourceConfigurationConstants.java: Loading...
  3. flume-ng-doc/sphinx/FlumeUserGuide.rst: Loading...