001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.jfreereport.wizard.utility;
014:
015: import java.io.BufferedReader;
016: import java.io.DataOutputStream;
017: import java.io.File;
018: import java.io.FileInputStream;
019: import java.io.InputStreamReader;
020: import java.net.ServerSocket;
021: import java.net.Socket;
022: import java.util.StringTokenizer;
023:
024: public class TinyHTTPd implements Runnable {
025: public static String solutionRoot = "./"; //$NON-NLS-1$
026: public static boolean die = false;
027: Socket c;
028:
029: public TinyHTTPd(Socket socket) {
030: c = socket;
031: }
032:
033: public static void startServer(String solutionRoot, final int port) {
034: TinyHTTPd.solutionRoot = solutionRoot;
035: die = false;
036: Runnable r = new Runnable() {
037: public void run() {
038: try {
039: ServerSocket serverSocket = new ServerSocket(port);
040: serverSocket.setSoTimeout(500);
041: while (!die) {
042: try {
043: Socket client = serverSocket.accept();
044: Thread t = new Thread(new TinyHTTPd(client));
045: t.start();
046: } catch (Exception e) {
047: }
048: }
049: } catch (Exception e) {
050: e.printStackTrace();
051: }
052: }
053: };
054: Thread t = new Thread(r);
055: t.setDaemon(true);
056: t.start();
057: }
058:
059: public static void stopServer() {
060: die = true;
061: }
062:
063: public static void main(String args[]) {
064: startServer("./resources/solutions", 6736); //$NON-NLS-1$
065: }
066:
067: public void run() {
068: try {
069: BufferedReader i = new BufferedReader(
070: new InputStreamReader(c.getInputStream()));
071: DataOutputStream o = new DataOutputStream(c
072: .getOutputStream());
073: try {
074: while (true) {
075: String s = i.readLine();
076: if (s.length() < 1)
077: break;
078: if (s.startsWith("GET")) { //$NON-NLS-1$
079: StringTokenizer t = new StringTokenizer(s, " "); //$NON-NLS-1$
080: t.nextToken();
081: String p = t.nextToken();
082: p = solutionRoot
083: + "/system/tmp/" + p.substring(p.indexOf("=") + 1); //$NON-NLS-1$ //$NON-NLS-2$
084: File file = new File(p);
085: int l = (int) new File(p).length();
086: byte[] b = new byte[l];
087: FileInputStream f = new FileInputStream(file);
088: f.read(b);
089: o
090: .writeBytes("HTTP/1.0 200 OK\nContent-Length:" + l + "\n\n"); //$NON-NLS-1$ //$NON-NLS-2$
091: o.write(b, 0, l);
092: }
093: }
094: } catch (Exception e) {
095: o.writeBytes("HTTP/1.0 404 ERROR\n\n\n"); //$NON-NLS-1$
096: }
097: o.close();
098: } catch (Exception e) {
099: }
100: }
101: }
|