Skip to content

Commit

Permalink
Merge branch 'jetty-9' to master
Browse files Browse the repository at this point in the history
  • Loading branch information
christophd committed Jul 3, 2015
2 parents cfa74a9 + 5eebbc4 commit 2529494
Show file tree
Hide file tree
Showing 20 changed files with 210 additions and 126 deletions.
43 changes: 31 additions & 12 deletions modules/citrus-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,31 @@
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-websocket</artifactId>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>javax-websocket-server-impl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down Expand Up @@ -317,22 +334,24 @@
</plugin>

<plugin>
<groupId>org.mortbay.jetty</groupId>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<webAppConfig>
<contextPath>/citrus-admin</contextPath>
</webAppConfig>
<connectors>
<connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
<port>${citrus.serverPort}</port>
<maxIdleTime>60000</maxIdleTime>
</connector>
</connectors>
<httpConnector>
<port>${citrus.serverPort}</port>
<idleTimeout>60000</idleTimeout>
</httpConnector>
<stopPort>${citrus.stopPort}</stopPort>
<stopKey>stop</stopKey>
<scanIntervalSeconds>5</scanIntervalSeconds>
<systemProperties>
<systemProperty>
<name>file.encoding</name>
<value>${project.build.sourceEncoding}</value>
</systemProperty>
<systemProperty>
<name>file.encoding</name>
<value>${project.build.sourceEncoding}</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.consol.citrus.admin.websocket;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.*;
import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -30,16 +31,18 @@
* @author Martin.Maher@consol.de
* @since 1.3
*/
public class LoggingWebSocket implements WebSocket.OnTextMessage {
@WebSocket
public class LoggingWebSocket {

/** Logger */
/**
* Logger
*/
private static final Logger LOG = LoggerFactory.getLogger(LoggingWebSocket.class);

/**
* Web Socket connections
* TODO MM thread safe
* Web Socket sessions
*/
private List<Connection> connections = new ArrayList<Connection>();
private static final Set<Session> sessions = Collections.synchronizedSet(new HashSet<Session>());

/**
* Default constructor.
Expand All @@ -55,65 +58,67 @@ public void run() {
timer.schedule(task, 60000, 60000);
}

/**
* {@inheritDoc}
*/
public void onOpen(Connection connection) {
LOG.info("Accepted a new connection");
this.connections.add(connection);
@SuppressWarnings("unused")
@OnWebSocketConnect
public void handleConnect(Session session) {
LOG.info("Accepted a new session");
sessions.add(session);
}

/**
* {@inheritDoc}
*/
@SuppressWarnings({"PMD.CloseResource"})
public void onClose(int closeCode, String message) {
LOG.debug("Web socket connection closed");
Iterator<Connection> itor = connections.iterator();
while (itor.hasNext()) {
Connection connection = itor.next();
if (connection == null || !connection.isOpen()) {
itor.remove();
@SuppressWarnings({"PMD.CloseResource", "unused"})
@OnWebSocketClose
public void handleClose(int statusCode, String reason) {
LOG.info(String.format("Closing session (%s:%s)", statusCode, reason));
for (Session session : sessions) {
if (session == null || !session.isOpen()) {
sessions.remove(session);
}
}
}

@SuppressWarnings("unused")
@OnWebSocketMessage
public void handleMessage(String message) {
LOG.info(String.format("Received message from client: %s", message));
}

/**
* {@inheritDoc}
*/
public void onMessage(String data) {
LOG.info("Received web socket client message: " + data);
@SuppressWarnings("unused")
@OnWebSocketError
public void handleError(Throwable error) {
LOG.warn("Error in websocket", error);
}

/**
* Send ping event.
*/
@SuppressWarnings("unchecked")
public void ping() {
private void ping() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("event", SocketEvent.PING.name());
push(jsonObject);
}


/**
* Push event to connected clients.
* @param event
*
* @param event the event to send to the connected clients
*/
@SuppressWarnings({"PMD.CloseResource"})
protected void push(JSONObject event) {
Iterator<Connection> itor = connections.iterator();
while (itor.hasNext()) {
Connection connection = itor.next();
if (connection != null && connection.isOpen()) {
try {
connection.sendMessage(event.toString());
} catch (IOException e) {
LOG.error("Error sending message", e);
}
} else {
itor.remove();
for (Session session : sessions) {
sendToSession(session, event);
}
}

private void sendToSession(Session session, JSONObject event) {
try {
if (session != null && session.isOpen()) {
session.getRemote().sendString(event.toString());
}
} catch (IOException e) {
LOG.error("Error sending message", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.consol.citrus.admin.websocket;

import org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest;
import org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.springframework.beans.factory.annotation.Autowired;

/**
* Created by Martin.Maher@consol.de on 19/06/15.
*/
public class LoggingWebSocketCreator implements WebSocketCreator {

@Autowired
private LoggingWebSocket loggingWebSocket;

@Override
public Object createWebSocket(ServletUpgradeRequest req, ServletUpgradeResponse resp) {
return loggingWebSocket;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.consol.citrus.admin.websocket;

import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketCreator;
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

/**
* Logging WebSocket
Expand All @@ -36,26 +36,23 @@ public class LoggingWebSocketServlet extends WebSocketServlet {

private static final Logger LOG = LoggerFactory.getLogger(LoggingWebSocketServlet.class);

private LoggingWebSocket loggingWebSocket;
private WebSocketCreator webSocketCreator;

@Override
public void init() throws ServletException {
super.init();
loggingWebSocket = getLoggingWebSocket();
public void configure(WebSocketServletFactory factory) {
LOG.info("Configuring the websocket");
factory.setCreator(webSocketCreator);
}

/**
* {@inheritDoc}
*/
public WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
LOG.info("Accepted a new connection");
return loggingWebSocket;
@Override
public void init() throws ServletException {
LOG.info("Initialising the websocket");
webSocketCreator = getWebSocketCreator();
super.init();
}

private LoggingWebSocket getLoggingWebSocket() {
private WebSocketCreator getWebSocketCreator() {
WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
return springContext.getBean(LoggingWebSocket.class);
return springContext.getBean(LoggingWebSocketCreator.class);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ enum SocketEvent {

/**
* Creates proper JSON message for socket event.
* @param processId
* @param pushEvent
* @param message
* @return
* @param processId the process id
* @param pushEvent the type of event
* @param message the event message
* @return a json representation of the message
*/
@SuppressWarnings("unchecked")
public static JSONObject createEvent(String processId, SocketEvent pushEvent, String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ public void onProcessFail(String processId, Throwable e) {

/**
* Setter for dependency injection of loggingWebSocket instance.
* @param loggingWebSocket
* @param loggingWebSocket the logging web socket
*/
public void setLoggingWebSocket(LoggingWebSocket loggingWebSocket) {
this.loggingWebSocket = loggingWebSocket;
}

/**
* Checks if output line is normal log outpu in log4j format.
* @param output
* @return
* Checks if output line is normal log output in log4j format.
* @param output the output to check
* @return true if normal log4j output
*/
private boolean isProcessOutputLine(String output) {
return output.contains("INFO") || output.contains("DEBUG") || output.contains("ERROR") || output.contains("WARN") || output.contains("TRACE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@
<!-- Central web socket pushes events to connected clients -->
<bean id="loggingWebSocket" class="com.consol.citrus.admin.websocket.LoggingWebSocket" scope="singleton"/>

<bean id="loggingWebSocketCreator" class="com.consol.citrus.admin.websocket.LoggingWebSocketCreator" scope="singleton"/>

<bean class="com.consol.citrus.admin.websocket.WebSocketTestListener"/>
<bean class="com.consol.citrus.admin.websocket.WebSocketProcessListener"/>
<bean class="com.consol.citrus.admin.websocket.WebSocketLoggingAppender"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public HttpClient() {
* Default constructor using endpoint configuration.
* @param endpointConfiguration
*/
protected HttpClient(HttpEndpointConfiguration endpointConfiguration) {
public HttpClient(HttpEndpointConfiguration endpointConfiguration) {
super(endpointConfiguration);

this.correlationManager = new PollingCorrelationManager(endpointConfiguration, "Reply message did not arrive yet");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.consol.citrus.exceptions.CitrusRuntimeException;
import org.springframework.util.FileCopyUtils;

/**
Expand Down Expand Up @@ -61,6 +63,21 @@ private RequestCachingInputStream() {
this.is = new ByteArrayInputStream(body);
}

@Override
public boolean isFinished() {
return is.available() == 0;
}

@Override
public boolean isReady() {
return true;
}

@Override
public void setReadListener(ReadListener readListener) {
throw new CitrusRuntimeException("Unsupported operation");
}

@Override
public int read() throws IOException {
return is.read();
Expand Down
Loading

0 comments on commit 2529494

Please sign in to comment.