Permalink
Browse files

Initial version of java-demo

  • Loading branch information...
0 parents commit 9ead771d08c9a75a92c7467f9e9cde38bf536376 @starksm64 starksm64 committed Apr 10, 2012
@@ -0,0 +1,8 @@
+#!/bin/bash
+# The logic to start up your application should be put in this
+# script. The application will work only if it binds to
+# $OPENSHIFT_INTERNAL_IP:8080
+
+cd $OPENSHIFT_REPO_DIR
+nohup java -cp bin test.MyHttpServer >${OPENSHIFT_LOG_DIR}/MyHttpServer.log 2>&1 &
+
@@ -0,0 +1,3 @@
+#!/bin/bash
+# The logic to stop your application should be put in this script.
+wget http://${OPENSHIFT_INTERNAL_IP}:${OPENSHIFT_INTERNAL_PORT}?action=stop
@@ -0,0 +1,20 @@
+This is a simple DIY cartridge demo that illustrates the use of java in the
+DIY application repo. The application consists of a single test.MyHttpServer main class that is located in the repository bin directory. The src for the class is in the src directory. To update the class run by the DIY cartridge you would simple recompile it using:
+
+javac -g -d bin src/test/MyHttpServer.java
+
+# First, create the DIY java application
+Assuming you have an OpenShift account and domain setup, and client tools installed (if you don't, see https://openshift.redhat.com/app/getting_started), you create your DIY application using:
+
+[8](ironmaiden:tmp) > rhc app create -t diy-0.1 -a diy -p *
+Creating application: diy
+Now your new domain name is being propagated worldwide (this might take a minute)...
+ retry # 5 - Waiting for DNS: diy-jbossdev.rhcloud.com
+Confirming application 'diy' is available: Success!
+
+diy published: http://diy-jbossdev.rhcloud.com/
+git url: ssh://0baa01674cac4a96a8ef8a1096867680@diy-jbossdev.rhcloud.com/~/git/diy.git/
+Disclaimer: This is an experimental cartridge that provides a way to try unsupported languages, frameworks, and middleware on Openshift.
+
+# Second, pull replace your app repository with the java-demo contents
+git remote add origin git@github.com:openshift/openshift-diy-java-demo.git
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
+<html>
+<body>
+<h1>Hello DIY World!</h1>
+</body>
+</html>
@@ -0,0 +1,150 @@
+package test;
+
+import com.sun.net.httpserver.Headers;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import com.sun.net.httpserver.HttpServer;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.InetSocketAddress;
+import java.net.URI;
+import java.util.Date;
+import java.util.Properties;
+
+/**
+ * A little jdk http server for testing the diy OpenShift cartridge with java
+ * @author Scott stark (sstark@redhat.com) (C) 2011 Red Hat Inc.
+ * @version $Revision:$
+ */
+public class MyHttpServer {
+ private static HttpServer server;
+
+ static class HtmlHandler implements HttpHandler {
+ private File root;
+ HtmlHandler(File root) {
+ this.root = root;
+ }
+ public void handle(HttpExchange he) throws IOException {
+ System.out.printf("Begin ------ %s\n", new Date());
+ try {
+ String action = writeHeaders(he);
+ String file = getRequestFile(he.getRequestURI());
+ String rootPath = root.getAbsolutePath();
+ if(action != null && action.equalsIgnoreCase("stop")) {
+ he.sendResponseHeaders(HttpURLConnection.HTTP_ACCEPTED, -1);
+ he.close();
+ System.out.print("Stopping server...");
+ server.stop(1);
+ System.out.println("done");
+ System.out.flush();
+ } else {
+ System.out.printf("Reading file: %s%s\n", rootPath, file);
+ writeFile(new File(root, file), he);
+ he.close();
+ }
+ System.out.printf("End ------ %s\n", new Date());
+ } catch (Throwable t) {
+ System.out.println("Internal error: ");
+ t.printStackTrace();
+ he.sendResponseHeaders(HttpURLConnection.HTTP_INTERNAL_ERROR, -1);
+ System.out.printf("End ------ %s\n", new Date());
+ }
+ }
+ String writeHeaders(HttpExchange he) {
+ String method = he.getRequestMethod();
+ URI requestURI = he.getRequestURI();
+ String query = requestURI.getQuery();
+ System.out.printf("RequestURI: %s\n", requestURI);
+ System.out.printf("RequestURI.Path: %s\n", requestURI.getPath());
+ System.out.printf("RequestURI.Query: %s\n", query);
+ String action = null;
+ Properties queryParams = new Properties();
+ if(query != null) {
+ // action=x,...
+ System.out.printf("Parsing query string: %s\n",query);
+ String[] params = query.split("[=,]");
+ for(int n = 0; n < params.length; n += 2) {
+ queryParams.setProperty(params[n].toLowerCase(), params[n+1]);
+ }
+ action = queryParams.getProperty("action");
+ if(action != null) {
+ System.out.printf("Found action: %s\n",action);
+ }
+ }
+ System.out.printf("RequestMethod: %s\n", method);
+ System.out.printf("Protocol: %s\n", he.getProtocol());
+ System.out.printf("RemoteAddress: %s\n", he.getRemoteAddress());
+ System.out.printf("LocalAddress: %s\n", he.getLocalAddress());
+ System.out.printf("ContextPath: %s\n", he.getHttpContext().getPath());
+ System.out.printf("ContextAttributes: %s\n", he.getHttpContext().getAttributes());
+ Headers headers = he.getRequestHeaders();
+ for(String key : headers.keySet()) {
+ System.out.printf("Header(%s): %s\n", key, headers.getFirst(key));
+ }
+ return action;
+ }
+ String getRequestFile(URI requestURI) {
+ String file = requestURI.getPath();
+ return file;
+ }
+ void writeFile(File path, HttpExchange he) throws IOException {
+ OutputStream os = he.getResponseBody();
+ he.getRequestBody().close();
+ if(path.exists() == false) {
+ he.sendResponseHeaders(HttpURLConnection.HTTP_NOT_FOUND, -1);
+ os.close();
+ System.out.printf("File not found: %s\n", path.getCanonicalPath());
+ return;
+ } else if(path.isDirectory()) {
+ // TODO: could look for welcome type files...
+ he.sendResponseHeaders(HttpURLConnection.HTTP_UNSUPPORTED_TYPE, -1);
+ os.close();
+ System.out.printf("File is a directory: %s\n", path.getCanonicalPath());
+ return;
+ }
+ System.out.printf("Sending file: %s,%d\n", path.getCanonicalPath(), path.length());
+ he.sendResponseHeaders(HttpURLConnection.HTTP_OK, path.length());
+
+ FileInputStream fis = new FileInputStream(path);
+ byte[] tmp = new byte[1024];
+ int length = fis.read(tmp);
+ while(length > 0) {
+ os.write(tmp, 0, length);
+ length = fis.read(tmp);
+ }
+ os.close();
+ fis.close();
+ }
+ }
+
+ public static void main(String[] args) throws Exception {
+ // Get various openshift environment variables
+ String repo = System.getenv("OPENSHIFT_REPO_DIR");
+ if(repo == null) {
+ repo = ".";
+ }
+ String ip = System.getenv("OPENSHIFT_INTERNAL_IP");
+ if(ip == null) {
+ ip = "localhost";
+ }
+ String ports = System.getenv("OPENSHIFT_INTERNAL_PORT");
+ if(ports == null) {
+ ports = "8080";
+ }
+ int port = Integer.decode(ports);
+ server = HttpServer.create(new InetSocketAddress(ip, port), 10);
+ File root = new File(repo+"/html");
+ if(root.exists() == false)
+ throw new FileNotFoundException(root.getAbsolutePath());
+ System.out.printf("Starting HttpServer for root context:%s\n", root.getCanonicalPath());
+ HtmlHandler handler = new HtmlHandler(root);
+ server.createContext("/", handler);
+ server.setExecutor(null); // creates a default executor
+ server.start();
+ }
+}

0 comments on commit 9ead771

Please sign in to comment.