Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract Servlet functionality into sse-common module for reuse #23

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
5a0e213
extract Servlet dependencies into shared class
jordanglassman Sep 18, 2017
8a3de16
split plugin into common and plugin modules
jordanglassman Sep 19, 2017
9c9ed29
add .gitignored file
jordanglassman Sep 19, 2017
22ff745
update sample project to use refactored plugin
jordanglassman Sep 19, 2017
08d0942
add new unit tests for base class
jordanglassman Sep 19, 2017
03153da
extract js client into standalone module
jordanglassman Sep 19, 2017
00bea67
rename modules to be consistent with existing project structure
jordanglassman Sep 19, 2017
55c2897
add .gitignored file
jordanglassman Sep 19, 2017
9a6b96d
fix accidental reversion
jordanglassman Sep 19, 2017
d8bba61
update package.json to use local copy of client
jordanglassman Sep 20, 2017
46f2abe
remove local copy of sse-gateway on clean
jordanglassman Sep 20, 2017
77306c4
expose apis needed for reuse
jordanglassman Sep 20, 2017
2a6edc5
move sse-gateway dep into devDependencies
jordanglassman Sep 21, 2017
d5facf8
update poms and deps per code review comments
jordanglassman Sep 21, 2017
fdce9f8
expose additional subscription queue apis
jordanglassman Sep 21, 2017
c0549a9
add repo for resolving parent pom
jordanglassman Sep 21, 2017
0c00c3f
add EventFilter subclass to maintain backwards compatibility with cli…
jordanglassman Sep 28, 2017
e29206a
refactor to use refactored version of pubsub-light with no jenkins de…
jordanglassman Sep 28, 2017
ac1638a
add sse-servlet to sse-gateway project
jordanglassman Sep 28, 2017
84fd556
updates following reversion of pubsub light repackaging
jordanglassman Oct 5, 2017
817d849
update get endpoints to correctly handle missing paths and hyphens
jordanglassman Oct 5, 2017
cf2d60a
updates to allow for serialization
jordanglassman Nov 2, 2017
a097c1b
extract sse servlet into module
jordanglassman Nov 2, 2017
ab3f623
add redis config provider
jordanglassman Nov 2, 2017
e3ee6b0
minor IT adjustments
jordanglassman Nov 2, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 34 additions & 45 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
<version>2.10</version> <!-- CAUTION: do not go past 2.16 without doing something like https://github.com/jenkinsci/workflow-cps-plugin/pull/87 -->
<relativePath />
</parent>
<artifactId>sse-gateway</artifactId>

<artifactId>sse-gateway-parent</artifactId>
<version>1.16-SNAPSHOT</version>
<packaging>hpi</packaging>
<packaging>pom</packaging>

<properties>
<jenkins.version>2.2</jenkins.version>
Expand Down Expand Up @@ -51,49 +52,37 @@
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pubsub-light</artifactId>
<version>1.6</version>
</dependency>

<!-- Test deps -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-plugin-core</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pubsub-light</artifactId>
<version>1.6</version>
</dependency>

</dependencies>
<!-- Test deps -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-plugin-core</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<showDeprecation>true</showDeprecation>
<contextPath>/jenkins</contextPath>
<!-- TODO specify ${java.level} when JENKINS-20679 implemented -->
<disabledTestInjection>true</disabledTestInjection>
</configuration>
</plugin>
</plugins>
</build>

<modules>
<module>sse-common</module>
<module>sse-gateway-client</module>
<module>sse-gateway</module>
</modules>
</project>
34 changes: 34 additions & 0 deletions sse-common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>sse-gateway-parent</artifactId>
<version>1.16-SNAPSHOT</version>
</parent>

<artifactId>sse-common</artifactId>
<version>1.16-SNAPSHOT</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pubsub-light</artifactId>
</dependency>

<!-- Test deps -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.jenkinsci.plugins.ssegateway;

import org.jenkinsci.plugins.ssegateway.sse.EventDispatcher;
import org.jenkinsci.plugins.ssegateway.sse.EventDispatcherFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* SSE base functionality for web applications based on Servlet API.
*/
public class SseServletBase {
private static final Logger LOGGER = Logger.getLogger(SseServletBase.class.getName());

/**
* Re-initialize or create a {@link EventDispatcher} for the clientId associated with this request.
*
* @throws IOException
*/
public void initDispatcher(HttpServletRequest request, HttpServletResponse response) throws IOException {
String clientId = request.getParameter("clientId");

if (clientId == null) {
throw new IOException("No 'clientId' parameter specified in connect request.");
}

HttpSession session = request.getSession();
EventDispatcher dispatcher = EventDispatcherFactory.getDispatcher(clientId, session);

// If there was already a dispatcher with this ID, then remove
// all subscriptions from it and reuse the instance.
if (dispatcher != null) {
LOGGER.log(Level.FINE, "We already have a Dispatcher for clientId {0}. Removing all subscriptions on the existing Dispatcher instance and reusing it.", dispatcher.toString());
dispatcher.unsubscribeAll();
} else {
// Else create a new instance with this id.
EventDispatcherFactory.newDispatcher(clientId, session);
}

response.setStatus(HttpServletResponse.SC_OK);
}

/**
* Returns true if the given {@link SubscriptionConfigQueue.SubscriptionConfig} is ready to be enqueued.
*
* @throws IOException
*/
public boolean validateSubscriptionConfig(SubscriptionConfigQueue.SubscriptionConfig subscriptionConfig) throws IOException {
LOGGER.log(Level.FINE, "Processing configuration request. batchId={0}", subscriptionConfig.getBatchId());
return subscriptionConfig.getDispatcherId() != null && subscriptionConfig.hasConfigs();
}

/**
* Returns true if the given {@link SubscriptionConfigQueue.SubscriptionConfig} is successfully enqueued.
*
* @throws IOException
*/
public boolean enqueueSubscriptionConfig(SubscriptionConfigQueue.SubscriptionConfig subscriptionConfig) throws IOException {
// The requests are added to a queue and processed async. A
// status notification will be pushed to the client async.
return SubscriptionConfigQueue.add(subscriptionConfig);
}

/**
* Returns true if the dispatcherId associated with this request exists and a ping event is successfully dispatched to it.
*
* @throws IOException
*/
public boolean ping(HttpServletRequest request) throws IOException {
String dispatcherId = request.getParameter("dispatcherId");

if (dispatcherId != null) {
EventDispatcher dispatcher = EventDispatcherFactory.getDispatcher(dispatcherId, request.getSession());
if (dispatcher != null) {
try {
dispatcher.dispatchEvent("pingback", "ack");
} catch (ServletException e) {
LOGGER.log(Level.FINE, "Failed to send pingback to dispatcher " + dispatcherId + ".", e);
return false;
}
}
}

return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.jenkinsci.plugins.ssegateway.sse.EventDispatcherFactory;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -171,7 +171,7 @@ String getDispatcherId() {
return dispatcherId;
}

static SubscriptionConfig fromRequest(StaplerRequest request) throws IOException {
static SubscriptionConfig fromRequest(HttpServletRequest request) throws IOException {
JSONObject payload = Util.readJSONPayload(request);
SubscriptionConfig config = new SubscriptionConfig();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.apache.commons.io.IOUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
Expand All @@ -48,7 +48,7 @@ public class Util {
private Util() {
}

public static JSONObject readJSONPayload(StaplerRequest request) throws IOException {
public static JSONObject readJSONPayload(HttpServletRequest request) throws IOException {
String characterEncoding = request.getCharacterEncoding();

if (characterEncoding == null) {
Expand Down
Loading