jett GET
diff --git a/apps/fpcagent/BUCK b/apps/fpcagent/BUCK
index c4f1565..ef4f0b0 100644
--- a/apps/fpcagent/BUCK
+++ b/apps/fpcagent/BUCK
@@ -13,20 +13,17 @@
'//apps/restconf/api:onos-apps-restconf-api',
'//models/fpcagent:onos-models-fpcagent',
'//models/common:onos-models-common',
- ':jetty-servlet',
- ':jetty-annotations',
- ':jetty-server-new',
- ':jetty-io-new',
- ':jetty-http-new',
- ':jetty-util-new',
+ '//lib:jetty-servlet',
+ '//lib:jetty-server',
+ '//lib:jetty-io',
+ '//lib:jetty-http',
+ '//lib:jetty-util',
'//lib:javax.servlet-api',
':zeromq',
':json',
]
-TEST_DEPS = [
- '//lib:TEST_ADAPTERS',
- '//utils/osgi:onlab-osgi-tests',
+PROVIDED_DEPS = [
]
BUNDLES = [
@@ -34,11 +31,12 @@
'//lib:httpclient-osgi',
'//lib:httpcore-osgi',
'//lib:org.apache.httpcomponents.httpasyncclient-osgi',
+ '//lib:jetty-servlet',
+ '//lib:jetty-server',
+ '//lib:jetty-io',
+ '//lib:jetty-http',
+ '//lib:jetty-util',
'//lib:javax.servlet-api',
- ':jetty-server-new',
- ':jetty-io-new',
- ':jetty-http-new',
- ':jetty-util-new',
]
EXCLUDED_BUNDLES = [
@@ -55,9 +53,9 @@
'org.onosproject.protocols.restconfserver',
]
-osgi_jar_with_tests(
+osgi_jar(
deps = COMPILE_DEPS,
- test_deps = TEST_DEPS,
+ provided_deps = PROVIDED_DEPS,
)
onos_app(
@@ -89,56 +87,3 @@
visibility = [ 'PUBLIC' ],
)
-remote_jar (
- name = 'jetty-annotations',
- out = 'jetty-annotations-8.1.19.v20160209.jar',
- url = 'mvn:org.eclipse.jetty:jetty-annotations:jar:8.1.19.v20160209',
- sha1 = '649592e66a01b01956c19b80d87c03c8052413ee',
- maven_coords = 'org.eclipse.jetty:jetty-annotations:8.1.19.v20160209',
- visibility = [ 'PUBLIC' ],
-)
-
-remote_jar (
- name = 'jetty-servlet',
- out = 'jetty-servlet-8.1.19.v20160209.jar',
- url = 'mvn:org.eclipse.jetty:jetty-servlet:jar:8.1.19.v20160209',
- sha1 = '6872c3fc289de8f26a43b101741b33af36590cb4',
- maven_coords = 'org.eclipse.jetty:jetty-servlet:8.1.19.v20160209',
- visibility = [ 'PUBLIC' ],
-)
-
-remote_jar (
- name = 'jetty-server-new',
- out = 'jetty-server-9.4.8.v20171121.jar',
- url = 'mvn:org.eclipse.jetty:jetty-server:jar:9.4.8.v20171121',
- sha1 = '34614bd9a29de57ef28ca31f1f2b49a412af196d',
- maven_coords = 'org.eclipse.jetty:jetty-server:9.4.8.v20171121',
- visibility = [ 'PUBLIC' ],
-)
-
-remote_jar (
- name = 'jetty-io-new',
- out = 'jetty-io-9.4.8.v20171121.jar',
- url = 'mvn:org.eclipse.jetty:jetty-io:jar:9.4.8.v20171121',
- sha1 = 'd3fe2dfa62f52ee91ff07cb359f63387e0e30b40',
- maven_coords = 'org.eclipse.jetty:jetty-io:9.4.8.v20171121',
- visibility = [ 'PUBLIC' ],
-)
-
-remote_jar (
- name = 'jetty-http-new',
- out = 'jetty-http-9.4.8.v20171121.jar',
- url = 'mvn:org.eclipse.jetty:jetty-http:jar:9.4.8.v20171121',
- sha1 = '9879d6c4e37400bf43f0cd4b3c6e34a3ba409864',
- maven_coords = 'org.eclipse.jetty:jetty-http:9.4.8.v20171121',
- visibility = [ 'PUBLIC' ],
-)
-
-remote_jar (
- name = 'jetty-util-new',
- out = 'jetty-util-9.4.8.v20171121.jar',
- url = 'mvn:org.eclipse.jetty:jetty-util:jar:9.4.8.v20171121',
- sha1 = 'd6ec1a1613c7fa72aa6bf5d8c204750afbc3df3b',
- maven_coords = 'org.eclipse.jetty:jetty-util:9.4.8.v20171121',
- visibility = [ 'PUBLIC' ],
-)
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventClient.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventClient.java
deleted file mode 100644
index 88b0201..0000000
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventClient.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- * Copyright © 2016 - 2017 Copyright (c) Sprint, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.onosproject.fpcagent.util.eventStream;
-
-import org.apache.commons.lang.exception.ExceptionUtils;
-import org.apache.http.HttpResponse;
-import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
-import org.apache.http.impl.nio.client.HttpAsyncClients;
-import org.apache.http.nio.IOControl;
-import org.apache.http.nio.client.methods.AsyncCharConsumer;
-import org.apache.http.nio.client.methods.HttpAsyncMethods;
-import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
-import org.apache.http.protocol.HttpContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.nio.CharBuffer;
-import java.util.AbstractMap;
-import java.util.ArrayList;
-
-/**
- * A HTTP client that sends a request to a FPC Client to initiate the request stream.
- */
-public class EventClient {
- private static final Logger log = LoggerFactory.getLogger(EventClient.class);
-
- private static final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
- private static ArrayList<String> clientUriList = new ArrayList<String>();
- protected String clientUri;
-
- /**
- * Send HttpRequest to Client
- *
- * @param uri - FPC Client Uri
- */
- public void connectToClient(String uri) {
- this.clientUri = uri;
- log.info("here connectToClient");
- try {
- client.start();
- HttpAsyncRequestProducer get = HttpAsyncMethods.createGet(this.clientUri);
- client.execute(get, new MyResponseConsumer(this.clientUri), null);
- } catch (Exception e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- }
-
- /**
- * A character consumer to read incoming characters on the request stream
- */
- static class MyResponseConsumer extends AsyncCharConsumer<Boolean> {
- private String clientUri;
-
- /**
- * Constructor
- *
- * @param clientUri - URI of the FPC Client
- */
- public MyResponseConsumer(String clientUri) {
- this.clientUri = clientUri;
- }
-
- @Override
- protected void onResponseReceived(final HttpResponse response) {
- }
-
- @Override
- protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) {
- char[] charArray = new char[buf.remaining()];
- System.arraycopy(buf.array(), 0, charArray, 0, buf.remaining());
- CharBuffer charBuffer = CharBuffer.wrap(charArray);
- ParseStream.getInstance().send(new AbstractMap.SimpleEntry<>(clientUri, charBuffer));
- }
-
- @Override
- protected void releaseResources() {
- }
-
- @Override
- protected Boolean buildResult(final HttpContext context) {
- return Boolean.TRUE;
- }
-
- }
-}
-
-
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventServer.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventServer.java
deleted file mode 100644
index ddb7426..0000000
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/EventServer.java
+++ /dev/null
@@ -1,99 +0,0 @@
-/*
- * Copyright © 2016 - 2017 Copyright (c) Sprint, Inc. and others. All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.onosproject.fpcagent.util.eventStream;
-
-import org.apache.commons.lang.exception.ExceptionUtils;
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.servlet.AsyncContext;
-import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-/**
- * A Http Servlet to server response streams
- */
-public class EventServer extends HttpServlet {
- private static final Logger log = LoggerFactory.getLogger(EventServer.class);
- private static ArrayList<String> clientUriList = new ArrayList<String>();
-
- /**
- * Method for stream initialization
- *
- * @param clientUri - Client Uri
- * @param request - The servlet request object
- * @param response - The servlet Response Object
- */
- private void init(String clientUri, HttpServletRequest request, HttpServletResponse response) {
- log.info("Response Stream Inititated");
- try {
- HttpSession session = request.getSession();
- session.setMaxInactiveInterval(72 * 60 * 60);
- EventClient client = new EventClient();
- client.connectToClient(clientUri);
- response.setHeader("Content-Type", "text/event-stream");
- response.setHeader("Cache-Control", "no-cache, no-store");
- response.setHeader("Connection", "keep-alive");
- AsyncContext asyncContext = request.startAsync(request, response);
- asyncContext.setTimeout(72 * 60 * 60 * 1000);
- asyncContext.getResponse().setBufferSize(1200);
- try {
- asyncContext.getResponse().flushBuffer();
- } catch (IOException e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- ServletContext servletContext = request.getServletContext();
- Map<String, AsyncContext> responseStreams = (ConcurrentHashMap<String, AsyncContext>) servletContext.getAttribute("responseStreams");
- responseStreams.put(clientUri, asyncContext);
- } catch (Exception e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- }
-
- protected void doPost(HttpServletRequest request, HttpServletResponse response) {
- log.info("here doPost");
- try {
- String clientUri = null;
- StringBuffer jsonStringBuilder = new StringBuffer();
- String line = null;
- try {
- BufferedReader br = request.getReader();
- while ((line = br.readLine()) != null)
- jsonStringBuilder.append(line);
- } catch (Exception e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
-
- try {
- if (jsonStringBuilder.length() > 0) {
- JSONObject jsonObj = new JSONObject(jsonStringBuilder.toString());
- clientUri = jsonObj.getString("client-uri");
- if (!clientUriList.contains(clientUri)) {
- init(clientUri, request, response);
- }
- jsonStringBuilder.setLength(0);
- }
- } catch (JSONException e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- } catch (Exception e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
-
- }
-}
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/JettyServer.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/JettyServer.java
index 03c610e..e6d11d1 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/JettyServer.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/JettyServer.java
@@ -10,6 +10,7 @@
import org.apache.commons.lang.exception.ExceptionUtils;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
+import org.eclipse.jetty.servlet.ServletHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +21,7 @@
* SSE Server implementation
*/
public class JettyServer implements AutoCloseable {
- private static final Logger log = LoggerFactory.getLogger(JettyServer.class);
+ private static final Logger log = LoggerFactory.getLogger(JettyServer.class);
private static JettyServer _instance;
private Server server;
@@ -34,38 +35,40 @@
public static JettyServer getInstance() {
return _instance;
}
- /**
- * Method used to initialize and start the SSE server
- */
- public void open() {
- ExecutorService executorService = Executors.newSingleThreadExecutor();
- executorService.submit(() -> {
- ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
- context.setContextPath("/");
- server = new Server(8070);
-// log.info("a");
- server.setHandler(context);
+ /**
+ * Method used to initialize and start the SSE server
+ */
+ public void open() {
+ ExecutorService executorService = Executors.newSingleThreadExecutor();
+ executorService.submit(() -> {
+ try {
+ server = new Server(8070);
+ ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
+ context.setContextPath("/");
-// log.info("a");
- context.addServlet(EventServer.class, "/response");
- context.addServlet(NotificationServer.class, "/notification");
-
-// log.info("a");
- context.addEventListener(new ConfigureService());
- context.addEventListener(new NotificationService());
+ context.addServlet(new ServletHolder(ResponseServer.class), "/response");
+ context.addServlet(new ServletHolder(NotificationServer.class), "/notification");
- try {
- server.start();
- log.info("Jetty-Server Started");
- server.join();
- } catch (Exception e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- } finally {
- server.destroy();
- }
- });
- }
+ server.setHandler(context);
+
+ context.addEventListener(new ConfigureService());
+ context.addEventListener(new NotificationService());
+
+ try {
+ server.start();
+ log.info("Jetty-Server Started");
+ server.join();
+ } catch (Exception e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ } finally {
+ server.destroy();
+ }
+ } catch (Exception e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ }
+ });
+ }
@Override
public void close() {
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/NotificationServer.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/NotificationServer.java
index e859cbf..ccbd364 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/NotificationServer.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/util/eventStream/NotificationServer.java
@@ -8,6 +8,7 @@
package org.onosproject.fpcagent.util.eventStream;
import org.apache.commons.lang.exception.ExceptionUtils;
+import org.eclipse.jetty.servlet.DefaultServlet;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
@@ -15,7 +16,6 @@
import javax.servlet.AsyncContext;
import javax.servlet.ServletContext;
-import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@@ -27,45 +27,66 @@
/**
* A HTTP servlet that handles requests for notification streams
*/
-public class NotificationServer extends HttpServlet {
- private static final Logger log = LoggerFactory.getLogger(EventServer.class);
+public class NotificationServer extends DefaultServlet {
+ private static final Logger log = LoggerFactory.getLogger(ResponseServer.class);
- protected void doPost( HttpServletRequest request, HttpServletResponse response){
- log.info("Notification stream Inititated");
- String clientId = null;
- StringBuffer jsonStringBuilder = new StringBuffer();
- String line = null;
- try {
- BufferedReader br = request.getReader();
- while ((line = br.readLine()) != null)
- jsonStringBuilder.append(line);
- } catch (Exception e) { e.printStackTrace(); }
+ public NotificationServer() {
+ super();
+ log.info("Notification Server Initialized");
+ }
- try {
- if(jsonStringBuilder.length() > 0){
- JSONObject jsonObj = new JSONObject(jsonStringBuilder.toString());
- clientId = jsonObj.getString("client-id");
- jsonStringBuilder.setLength(0);
- HttpSession session = request.getSession();
- session.setMaxInactiveInterval(72*60*60);
- response.setHeader("Content-Type", "text/event-stream");
- response.setHeader("Cache-Control", "no-cache, no-store");
- response.setHeader("Connection", "keep-alive");
- AsyncContext asyncContext = request.startAsync(request,response);
- asyncContext.setTimeout(72*60*60*1000);
- asyncContext.getResponse().setBufferSize(1200);
- try {
- asyncContext.getResponse().flushBuffer();
- } catch (IOException e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- ServletContext servletContext = request.getServletContext();
- Map<String,AsyncContext> notificationStreams = (ConcurrentHashMap<String,AsyncContext>) servletContext.getAttribute("notificationStreams");
- notificationStreams.put(clientId,asyncContext);
- log.info("Client Id received in the notification stream request: "+clientId);
- }
- } catch (JSONException e) {
- log.error(ExceptionUtils.getFullStackTrace(e));
- }
- }
+ @Override
+ protected void doGet(HttpServletRequest request, HttpServletResponse response) {
+ try {
+ response.setContentType("text/html");
+ response.setStatus(HttpServletResponse.SC_OK);
+ response.getWriter().println("<h1>Hello World</h1>");
+ response.getWriter().println("session=" + request.getSession(true).getId());
+ } catch (IOException e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest request, HttpServletResponse response) {
+ log.info("Notification stream Inititated");
+ String clientId = null;
+ StringBuffer jsonStringBuilder = new StringBuffer();
+ String line = null;
+ try {
+ BufferedReader br = request.getReader();
+ while ((line = br.readLine()) != null) {
+ jsonStringBuilder.append(line);
+ }
+ } catch (Exception e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ }
+
+ try {
+ if (jsonStringBuilder.length() > 0) {
+ JSONObject jsonObj = new JSONObject(jsonStringBuilder.toString());
+ clientId = jsonObj.getString("client-id");
+ jsonStringBuilder.setLength(0);
+ HttpSession session = request.getSession();
+ session.setMaxInactiveInterval(72 * 60 * 60);
+ response.setHeader("Content-Type", "text/event-stream");
+ response.setHeader("Cache-Control", "no-cache, no-store");
+ response.setHeader("Connection", "keep-alive");
+ AsyncContext asyncContext = request.startAsync(request, response);
+ asyncContext.setTimeout(72 * 60 * 60 * 1000);
+ asyncContext.getResponse().setBufferSize(1200);
+ try {
+ asyncContext.getResponse().flushBuffer();
+ } catch (IOException e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ }
+ ServletContext servletContext = request.getServletContext();
+ Map<String, AsyncContext> notificationStreams = (ConcurrentHashMap<String, AsyncContext>) servletContext.getAttribute("notificationStreams");
+ notificationStreams.put(clientId, asyncContext);
+ log.info("Client Id received in the notification stream request: " + clientId);
+ }
+ } catch (JSONException e) {
+ log.error(ExceptionUtils.getFullStackTrace(e));
+ }
+ }
}