protected HttpConnector createHttpConnector(ConnectorConfiguration connectorConfig, Router router) { return new HttpConnector(connectorConfig, router); }
protected HttpsConnector createHttpsConnector(ConnectorConfiguration connectorConfig, Router router) { return new HttpsConnector(connectorConfig, router); } }
public void start() { logger.info("Starting the connectors"); for (AbstractHttpConnector connector : connectors) { connector.start(); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest httpRequest) throws Exception { logger.trace("Received a {} message {} from the remote address {}", configuration.getScheme().name(), httpRequest, ctx.channel().remoteAddress()); URI baseUri = HttpUtil.createURI(configuration.getScheme().name(), httpRequest.headers().get(HttpHeaders.Names.HOST), "//"); MessageContext context = new MessageContext(httpRequest, baseUri); ctx.attr(MESSAGE_CONTEXT).set(context); listener.onReceived(context); router.route(context); listener.onSuccess(context); ctx.writeAndFlush(context.getResponse()).addListener(ChannelFutureListener.CLOSE); listener.onComplete(context); } }
protected String getMetricName(MessageContext context, String metricName) { String name = null; if (! Strings.isNullOrEmpty(context.getMatchedRoute())) { name = context.getMatchedRoute(); } else { name = context.getApplication().getConfiguration().getName(); } name = formatName(name); return MetricRegistry.name(name, context.getRequest().getMethod().toString(), metricName); }
@Override public void onComplete(MessageContext context) { String name = null; Boolean successful = (Boolean) context.getAttribute(SUCCESSFUL); if (successful == null) { name = MetricRegistry.name(context.getApplication().getConfiguration().getName(), EXCEPTIONS); MetricRegistries.getRegistry(context.getApplication().getConfiguration().getName()).meter(name).mark(); } else { if (! successful) { name = getMetricName(context, Integer.toString(context.getResponse().getStatus().code())); MetricRegistries.getRegistry(context.getApplication().getConfiguration().getName()).meter(name).mark(); } name = getMetricName(context, RESPONSE_TIME); Timer timer = MetricRegistries.getRegistry(context.getApplication().getConfiguration().getName()).timer(name); timer.update(clock.getTick() - (Long) context.getAttribute(START_TIME), TimeUnit.NANOSECONDS); } } }
/** * Creates the container request from the http request * * @param httpRequest * @return */ protected ContainerRequest createContainerRequest(MessageContext context) { Application<ApplicationConfiguration> application = context.getApplication(); FullHttpRequest httpRequest = context.getRequest(); URI baseUri = URI.create(context.getBaseUri().resolve(application.getPath()) + "/"); URI requestUri = HttpUtil.createURI(httpRequest.getUri()); ContainerRequest containerRequest = new ContainerRequest(baseUri, requestUri, httpRequest.getMethod().name(), null, new MapPropertiesDelegate()); // containerRequest.setProperty(REQUEST_PROPERTY_REMOTE_ADDR, context.getRequest().channel().remoteAddress()); containerRequest.setEntityStream(new ByteBufInputStream(httpRequest.content())); for (Map.Entry<String, String> headerEntry : httpRequest.headers()) { containerRequest.getHeaders().add(headerEntry.getKey(), headerEntry.getValue()); } return containerRequest; }
logger.trace("Routing the context {}", context); Application<ApplicationConfiguration> application = applicationMapping.resolve(context.getRequest()); if (application == null) { throw new NotFoundException("Request path not found"); context.setApplication(application); if (listener != null) { listener.onApplicationResolved(context); List<String> matchedUris = uriInfo.getMatchedURIs(); if (matchedUris != null && ! matchedUris.isEmpty()) { context.setMatchedRoute(matchedUris.get(0)); context.setResponse(httpResponse);
public void init(Container container, ServerBundleConfiguration config) { logger.info("Initializing the container"); // Override the supplied one ServerConfiguration configuration = container.getConfiguration().getServerConfiguration(); AbstractHttpConnector connector = null; InternalLoggerFactory.setDefaultFactory(new Slf4JLoggerFactory()); logger.info("Loading the http connectors"); for (ConnectorConfiguration connectorConfig : configuration.getConnectorConfigurations()) { if (connectorConfig.getScheme() == Scheme.https) { connector = createHttpsConnector(connectorConfig, container.getRouter()); } else { connector = createHttpConnector(connectorConfig, container.getRouter()); } connector.registerListener(container.getMessageObserver()); connector.initialize(); connectors.add(connector); } }
@Override public void onSuccess(MessageContext context) { context.addAttribute(SUCCESSFUL, context.getResponse().getStatus().code() < 400); }
@Override public void onApplicationResolved(MessageContext context) { ApplicationContext.instance().setApplicationConfiguration(context.getApplication().getConfiguration()); for (final MessageListener listener : messageListeners) { invoke(new ListenerInvoker(){ @Override public void invoke(MessageContext context) { listener.onApplicationResolved(context); } }, context); } }
FullHttpRequest httpRequest = context.getRequest(); DefaultFullHttpResponse httpResponse = new DefaultFullHttpResponse(httpRequest.getProtocolVersion(), HttpResponseStatus.valueOf(containerResponse.getStatus()), buffer); int length = containerResponse.getLength();
public void stop() { logger.info("Stopping the connectors"); for (AbstractHttpConnector connector : connectors) { connector.stop(); } }
@Override public void onReceived(MessageContext context) { context.addAttribute(START_TIME, clock.getTick()); }
@Override protected void addChannelHandlers(ChannelPipeline pipeline) { logger.debug("Adding ssl handler to the pipeline"); SSLEngine engine = createSslEngine(); engine.setUseClientMode(false); pipeline.addFirst("ssl", new SslHandler(engine)); }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable e) throws Exception { logger.error("Exception caught in the http connector", e); if (ctx.attr(MESSAGE_CONTEXT).get() instanceof MessageContext) { listener.onError(ctx.attr(MESSAGE_CONTEXT).get()); } else { listener.onError(e.getCause()); } super.exceptionCaught(ctx, e); HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); }
@Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpRequestDecoder(), new HttpResponseEncoder(), new HttpObjectAggregator(configuration.getMaxContentLength()), AbstractHttpConnector.this); addChannelHandlers(ch.pipeline()); } });
SSLConfiguration configuration = getConnectorConfiguration().getSslConfiguration(); InputStream stream = null; try {