Browse Source

[FIX] initial commit

Gogs 7 years ago
commit
f3d78b2e2e

+ 4 - 0
.gitignore

@@ -0,0 +1,4 @@
+.classpath
+.git
+.project
+.settings

+ 49 - 0
pom.xml

@@ -0,0 +1,49 @@
+<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>
+
+  <groupId>org.robert</groupId>
+  <artifactId>printers-tray</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>printer-tray</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+     <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-server -->
+	<dependency>
+	    <groupId>org.eclipse.jetty</groupId>
+	    <artifactId>jetty-server</artifactId>
+	    <version>9.4.7.v20170914</version>
+	</dependency>
+	<!-- https://mvnrepository.com/artifact/org.eclipse.jetty.websocket/websocket-server -->
+	<dependency>
+	    <groupId>org.eclipse.jetty.websocket</groupId>
+	    <artifactId>websocket-server</artifactId>
+	    <version>9.4.7.v20170914</version>
+	</dependency>
+	<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
+	<dependency>
+	    <groupId>commons-io</groupId>
+	    <artifactId>commons-io</artifactId>
+	    <version>2.5</version>
+	</dependency>
+	<!-- https://mvnrepository.com/artifact/org.codehaus.jettison/jettison -->
+	<dependency>
+	    <groupId>org.codehaus.jettison</groupId>
+	    <artifactId>jettison</artifactId>
+	    <version>1.3.8</version>
+	</dependency>
+  </dependencies>
+</project>

+ 27 - 0
src/main/java/org/robert/printer/App.java

@@ -0,0 +1,27 @@
+package org.robert.printer;
+
+import org.robert.printer.ws.PrintersSocketServer;
+
+/**
+ * Hello world!
+ *
+ */
+public class App {
+	/**
+	 *
+	 * @param args
+	 */
+	public static void main(String[] args) {
+		new Runnable() {
+			public void run() {
+				PrintersSocketServer server = new PrintersSocketServer();
+
+				try {
+					server.goUp();
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
+		};
+	}
+}

+ 26 - 0
src/main/java/org/robert/printer/common/PrintersDiscovery.java

@@ -0,0 +1,26 @@
+package org.robert.printer.common;
+
+import javax.print.PrintService;
+import javax.print.PrintServiceLookup;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+
+public abstract class PrintersDiscovery {
+
+	/**
+	 *
+	 * @return
+	 * @throws JSONException
+	 */
+	public static JSONObject getPrinterNames() throws JSONException {
+		JSONObject json = new JSONObject();
+		PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
+
+		for (PrintService service : services) {
+			json.append("printers", service.getName());
+		}
+
+		return json;
+	}
+}

+ 11 - 0
src/main/java/org/robert/printer/common/PrintingActions.java

@@ -0,0 +1,11 @@
+package org.robert.printer.common;
+
+public enum PrintingActions {
+
+	LIST("list", ""),
+	PRINT("print", "data");
+
+	PrintingActions(String actionName, String params) {
+
+	}
+}

+ 104 - 0
src/main/java/org/robert/printer/ws/PrintersSocketHandler.java

@@ -0,0 +1,104 @@
+package org.robert.printer.ws;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.util.HashMap;
+
+import org.apache.commons.io.IOUtils;
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.eclipse.jetty.websocket.api.Session;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError;
+import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
+import org.eclipse.jetty.websocket.api.annotations.WebSocket;
+import org.robert.printer.common.PrintersDiscovery;
+
+@WebSocket
+public class PrintersSocketHandler {
+
+	private final HashMap<Integer, SocketConnection> connections = new HashMap<Integer, SocketConnection>();
+
+	/**
+	 *
+	 * @return
+	 */
+	public HashMap<Integer, SocketConnection> getConnections() {
+		return this.connections;
+	}
+
+	/**
+	 *
+	 * @param session
+	 */
+	@OnWebSocketConnect
+	public void onConnect(Session session) {
+		this.getConnections().put(session.getRemoteAddress().getPort(), new SocketConnection());
+	}
+
+	/**
+	 *
+	 * @param session
+	 * @param code
+	 * @param reason
+	 */
+	@OnWebSocketClose
+	public void onClose(Session session, int code, String reason) {
+		int port = session.getRemoteAddress().getPort();
+		this.getConnections().remove(port);
+	}
+
+	/**
+	 *
+	 * @param session
+	 * @param error
+	 */
+	@OnWebSocketError
+	public void onError(Session session, Throwable error) {
+		System.out.println(error.getMessage());
+	}
+
+	@OnWebSocketMessage
+	public void onMessage(Session session, Reader reader) throws IOException, JSONException {
+		String message = IOUtils.toString(reader);
+
+		if (message == null || message.isEmpty()) {
+			return;
+		}
+
+		JSONObject json = new JSONObject(message);
+
+		int port = session.getRemoteAddress().getPort();
+		SocketConnection connection = this.getConnections().get(port);
+
+		this.processMessage(session, json, connection);
+	}
+
+	/**
+	 * @throws JSONException
+	 *
+	 */
+	private void processMessage(Session session, JSONObject json, SocketConnection connection) throws JSONException {
+		String action = json.optString("action");
+
+
+		if (action.equals("list")) {
+			JSONObject printerNames = PrintersDiscovery.getPrinterNames();
+			this.send(session, printerNames);
+		}
+	}
+
+	/**
+	 *
+	 * @param session
+	 * @param reply
+	 */
+	private synchronized void send(Session session, JSONObject reply) {
+		try {
+			session.getRemote().sendString(reply.toString());
+		} catch (IOException e) {
+			e.printStackTrace();
+		}
+	}
+}

+ 104 - 0
src/main/java/org/robert/printer/ws/PrintersSocketServer.java

@@ -0,0 +1,104 @@
+package org.robert.printer.ws;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.eclipse.jetty.server.Connector;
+import org.eclipse.jetty.server.HttpConfiguration;
+import org.eclipse.jetty.server.HttpConnectionFactory;
+import org.eclipse.jetty.server.Server;
+import org.eclipse.jetty.server.ServerConnector;
+import org.eclipse.jetty.websocket.server.WebSocketHandler;
+import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory;
+
+public class PrintersSocketServer {
+
+	private Server server;
+	private final String host;
+	private final int port;
+	private final AtomicBoolean running;
+
+	public PrintersSocketServer() {
+		this.host = "127.0.0.1";
+		this.port = 8700;
+		this.running = new AtomicBoolean(false);
+	}
+
+	/**
+	 *
+	 * @return
+	 */
+	private Server getServer() {
+		return this.server;
+	}
+
+	/**
+	 *
+	 * @return
+	 */
+	private String getHost() {
+		return this.host;
+	}
+
+	/**
+	 *
+	 * @return
+	 */
+	private int getPort() {
+		return this.port;
+	}
+
+	/**
+	 *
+	 * @return
+	 */
+	public boolean isRunning() {
+		return this.running.get();
+	}
+
+
+	/**
+	 * @throws Exception
+	 *
+	 */
+	public void goUp() throws Exception {
+		this.server = new Server(this.getPort());
+
+		HttpConnectionFactory httpConnectionFactory = new HttpConnectionFactory(new HttpConfiguration());
+
+		ServerConnector connector = new ServerConnector(this.server, httpConnectionFactory);
+		connector.setHost(this.getHost());
+		connector.setPort(this.getPort());
+
+		this.server.addConnector(connector);
+
+		final WebSocketHandler wsHandler = new WebSocketHandler() {
+			@Override
+			public void configure(WebSocketServletFactory factory) {
+				factory.register(PrintersSocketHandler.class);
+				factory.getPolicy().setMaxTextMessageSize(Integer.MAX_VALUE);
+			}
+		};
+
+		this.server.setHandler(wsHandler);
+		this.server.setStopAtShutdown(true);
+		this.server.start();
+
+		this.running.set(true);
+		this.server.join();
+	}
+
+	/**
+	 * @throws Exception
+	 *
+	 */
+	public void goDown() throws Exception {
+		this.getServer().getHandler().destroy();
+
+		for (Connector connector : this.getServer().getConnectors()) {
+			connector.stop();
+		}
+
+		this.getServer().stop();
+		this.running.set(false);
+	}
+}

+ 6 - 0
src/main/java/org/robert/printer/ws/SocketConnection.java

@@ -0,0 +1,6 @@
+package org.robert.printer.ws;
+
+public class SocketConnection {
+
+
+}

+ 38 - 0
src/test/java/org/robert/printer/AppTest.java

@@ -0,0 +1,38 @@
+package org.robert.printer;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest 
+    extends TestCase
+{
+    /**
+     * Create the test case
+     *
+     * @param testName name of the test case
+     */
+    public AppTest( String testName )
+    {
+        super( testName );
+    }
+
+    /**
+     * @return the suite of tests being tested
+     */
+    public static Test suite()
+    {
+        return new TestSuite( AppTest.class );
+    }
+
+    /**
+     * Rigourous Test :-)
+     */
+    public void testApp()
+    {
+        assertTrue( true );
+    }
+}

BIN
target/classes/org/robert/printer/App$1.class


BIN
target/classes/org/robert/printer/App.class


BIN
target/classes/org/robert/printer/common/PrintersDiscovery.class


BIN
target/classes/org/robert/printer/common/PrintingActions.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketHandler.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketServer$1.class


BIN
target/classes/org/robert/printer/ws/PrintersSocketServer.class


BIN
target/classes/org/robert/printer/ws/SocketConnection.class


BIN
target/test-classes/org/robert/printer/AppTest.class