Back to index

moin  1.9.0~rc2
TWikiDraw.java
Go to the documentation of this file.
00001 /*
00002  * @(#)TWikiDraw.java 5.1
00003  * Copyright 2000 by Peter Thoeny, Peter@Thoeny.com.
00004  * It is hereby granted that this software can be used, copied, 
00005  * modified, and distributed without fee provided that this 
00006  * copyright notice appears in all copies.
00007  * Portions Copyright (C) 2001 Motorola - All Rights Reserved
00008  */
00009 
00010 package CH.ifa.draw.twiki;
00011 
00012 import Acme.JPM.Encoders.*;
00013 import CH.ifa.draw.appframe.*;
00014 import CH.ifa.draw.framework.*;
00015 import CH.ifa.draw.standard.*;
00016 import CH.ifa.draw.figures.*;
00017 import CH.ifa.draw.util.*;
00018 import CH.ifa.draw.applet.*;
00019 import CH.ifa.draw.contrib.*;
00020 
00021 import java.applet.Applet;
00022 import java.awt.*;
00023 import java.awt.event.*;
00024 import java.lang.reflect.*;
00025 import java.util.*;
00026 import java.io.*;
00027 import java.net.*;
00028 
00029 public  class TWikiDraw extends LightweightDrawApplet {
00030 
00031     public void      init() {
00032        ColorMap.reset();
00033        if (getParameter("extracolors") != null) {
00034            String colors = getParameter("extracolors");
00035            // parse the colors string and add colors to the static color map
00036            do {
00037               String thisColor;
00038               int split = colors.indexOf(',');
00039               if (split == -1) {
00040                   thisColor = colors;
00041                   colors = "";
00042               } else {
00043                   thisColor = colors.substring(0, split);
00044                   colors = colors.substring(split + 1);
00045               }
00046               split = thisColor.indexOf('=');
00047               if (split == -1)
00048                   continue;
00049               String name = thisColor.substring(0, split).trim();
00050               if (split < thisColor.length() - 1 &&
00051                   thisColor.charAt(split + 1) == '#')
00052                   split++;
00053               String value = thisColor.substring(split + 1).trim();
00054               try {
00055                   int i = Integer.valueOf(value, 16).intValue();
00056                   ColorMap.getColorMap().addColor(name, new Color(i));
00057               } catch (NumberFormatException nfe) {
00058                   nfe.printStackTrace();
00059               }
00060            } while (colors.length() > 0);
00061        }
00062 
00063         init(new TWikiFrame(this));
00064        String drawPath = getParameter("drawpath");
00065        add(new Label("TWikiDraw editing " + drawPath));
00066     }
00067 
00071     public boolean post(
00072        String url,
00073        String fileName,
00074        String type,
00075        String path,
00076        String content,
00077        String comment)
00078        throws MalformedURLException, IOException {
00079 
00080        String sep = "89692781418184";
00081        while (content.indexOf(sep) != -1)
00082            sep += "x";
00083 
00084        String message = makeMimeForm(fileName, type,
00085                                   path, content, comment, sep);
00086 
00087         // for test
00088         //URL server = new URL("http", "localhost", 80, savePath);
00089         URL server = new URL(
00090               getCodeBase().getProtocol(),
00091            getCodeBase().getHost(),
00092            getCodeBase().getPort(),
00093            url);
00094         URLConnection connection = server.openConnection();
00095 
00096         connection.setAllowUserInteraction(false);
00097         connection.setDoOutput(true);
00098         //connection.setDoInput(true);
00099         connection.setUseCaches(false);
00100 
00101         connection.setRequestProperty(
00102            "Content-type",
00103            "multipart/form-data; boundary=" + sep);
00104         connection.setRequestProperty(
00105            "Content-length",
00106            Integer.toString(message.length()));
00107 
00108        //System.out.println(url);
00109        String replyString = null;
00110        try {
00111            DataOutputStream out =
00112               new DataOutputStream(connection.getOutputStream());
00113            out.writeBytes(message);
00114            out.close();
00115            //System.out.println("Wrote " + message.length() +
00116            //           " bytes to\n" + connection);
00117            
00118            try {
00119               BufferedReader in =
00120                   new BufferedReader(new InputStreamReader(
00121                      connection.getInputStream()));
00122               String reply = null;
00123               while ((reply = in.readLine()) != null) {
00124                   if (reply.startsWith("ERROR ")) {
00125                      replyString = reply.substring("ERROR ".length());
00126                   }
00127               }
00128               in.close();
00129            } catch (IOException ioe) {
00130               replyString = ioe.toString();
00131            }
00132        } catch (UnknownServiceException use) {
00133            replyString = use.getMessage();
00134            System.out.println(message);
00135        }
00136        if (replyString != null) {
00137            //System.out.println("---- Reply " + replyString);
00138             if (replyString.startsWith("URL ")) {
00139               URL eurl = getURL(replyString.substring("URL ".length()));
00140                 getAppletContext().showDocument(eurl);
00141             } else if (replyString.startsWith("java.io.FileNotFoundException")) {
00142               // debug; when run from appletviewer, the http connection
00143               // is not available so write the file content
00144               if (path.endsWith(".draw") || path.endsWith(".map"))
00145                   System.out.println(content);
00146            } else
00147                 showStatus(replyString);
00148            return false;
00149        } else {
00150            showStatus(url + " saved");
00151            return true;
00152        }
00153     }
00154 
00155     //-----------------------------------------------------------------------
00156 
00173     static String NL = "\r\n";
00174     static String NLNL = NL + NL;
00175 
00177     private String makeMimeForm(
00178        String fileName,
00179        String type,
00180        String path,
00181        String content,
00182        String comment,
00183        String sep) {
00184 
00185        String binary = "";
00186        if (type.equals("image/gif")) {
00187            binary = "Content-Transfer-Encoding: binary" + NL;
00188        }
00189 
00190        String mime_sep = NL + "--" + sep + NL;
00191 
00192        return
00193            "--" + sep + "\r\n" 
00194             + "Content-Disposition: form-data; name=\"filename\"" + NLNL
00195            + fileName
00196            + mime_sep
00197             + "Content-Disposition: form-data; name=\"noredirect\"" + NLNL
00198            + 1
00199            + mime_sep
00200             + "Content-Disposition: form-data; name=\"filepath\"; "
00201             + "filename=\"" + path + "\"" + NL
00202             + "Content-Type: " + type + NL
00203            + binary + NL
00204            + content
00205            + mime_sep
00206            + "Content-Disposition: form-data; name=\"filecomment\"" + NLNL
00207            + comment
00208            + NL + "--" + sep + "--" + NL;
00209     }
00210 
00212     void exitApplet() {
00213        String viewPath = getParameter(VIEWPATH_PARAMETER);
00214        if (viewPath != null) {
00215            try {
00216               URL url = getURL(viewPath);
00217               getAppletContext().showDocument(url, "_self");
00218            } catch (MalformedURLException mue) {
00219               showStatus("Bad URL for viewpath " + viewPath);
00220            }
00221        }
00222     }
00223 
00224     static private String VIEWPATH_PARAMETER   = "viewpath";
00225 }