AppletはWebページでリモートデスクトップフロントエンドテストコード【メモ】を開く


かつてAppletでWebページでリモートデスクトップのフロントエンドテストコードを開いていたが、バックグラウンドコードは公開できず、メモだけだった.
RdpApplet.java:
package com.elusiva.rdp.applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.TextArea;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import com.elusiva.rdp.Common;
import com.elusiva.rdp.OrderException;
import com.elusiva.rdp.RdesktopException;
import com.elusiva.rdp.RdesktopSwing;

public class RdpApplet extends Applet {
	private final String rdpShellPath = "C:\\Program Files\\XXX\\Open Virtual Desktop\\rdpshell.exe";
	private RdpThread_2 rThread = null;
	private String[] argArray = null;
	private List<String> argList = new ArrayList<String>();
    TextArea aTextArea = null;
    PrintStream aPrintStream  = null;
  
   public void paint(Graphics g){
       g.setColor(new Color(0xFFFFFF));
       g.fillRect(0,0,g.getClipBounds().width,g.getClipBounds().height);
       g.setColor(new Color(0x000000));
       int width = g.getFontMetrics().stringWidth("Launching isoft IVAPP Everywhere session...");
       int x = (int) (g.getClipBounds().getWidth() / 2) - (width/2);
       int y = (int) (g.getClipBounds().getHeight() / 2);
       if(!redirectOutput) g.drawString("Launching isoft IVAPP Everywhere session...", x, y);
       width = g.getFontMetrics().stringWidth("Connect to Server...");
       x = (int) (g.getClipBounds().getWidth() / 2) - (width/2);
       y = (int) (g.getClipBounds().getHeight() / 2) + 20;
       if(!redirectOutput) g.drawString("Connect to Server...", x, y);
   }
   
   boolean redirectOutput = false;
   
   public void init(){
       redirectOutput = isSet("redirectOutput");
       if(redirectOutput){
           aPrintStream = new PrintStream( new FilteredStream(new ByteArrayOutputStream() ) );
           System.setOut(aPrintStream);
           System.setErr(aPrintStream);
           aTextArea = new TextArea();
           setLayout(new BorderLayout());
           add("Center" , aTextArea);
       }
       
       initArgArray();
       //getargArray();
   }
   
	public void initArgArray() {
		String url = this.getParameter("url");
		System.out.println("url=" + url);
		Map<String, String> params = null;
		params = RequestServer.doRequest(url);
		//  
		argList.add("-u");
		argList.add("31" + params.get("loginName"));
		//  
		argList.add("-p");
		argList.add(params.get("loginPassword"));

		argList.add("-fsmc");
		// path
		argList.add("-s");
		argList.add(rdpShellPath+" "+params.get("appExePath"));
		
		// IP 
		argList.add(params.get("ip"));
		argArray = new String[argList.size()];
		Object[] objList = argList.toArray();
		int j = 0;
		for (Object o : objList) {
			argArray[j++] = o.toString();
			System.out.print(o.toString());
		}
	}
   
    public void start(){    
        Common.underApplet = true;
        rThread = new RdpThread_2(argArray, this.getParameter("redirect_on_exit"), this);
        rThread.start();
    }
    
    public void stop(){
        rThread = null;
        notify();
    }
    
    private boolean isSet(String parameter){
        String s = this.getParameter(parameter);
        if(s != null){
            if(s.equalsIgnoreCase("yes"))
                return true;
        }
        return false;
    }
    
    class FilteredStream extends FilterOutputStream {
        public FilteredStream(OutputStream aStream) {
           super(aStream);
           }

        public void write(byte b[]) throws IOException {
           String aString = new String(b);
           aTextArea.append(aString);
           }

        public void write(byte b[], int off, int len) throws IOException {
           String aString = new String(b , off , len);
           aTextArea.append(aString);
        }
     }
    
}

class RdpThread extends Thread{
     
    String[] args;
    String redirect = null;
    Applet parentApplet = null;
    
    public RdpThread(String[] args, String redirect, Applet a){
        parentApplet = a;
        this.args = args;
        this.redirect = redirect;
    }
    
    public void run(){
        this.setPriority(Thread.MAX_PRIORITY);
        try {
            RdesktopSwing.main(args);
       
            if(redirect != null){
               URL u = new URL(redirect);
               parentApplet.getAppletContext().showDocument(u);              
            }
        } catch (OrderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RdesktopException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            e.printStackTrace();
        }
        Common.underApplet = false;
    }
}

RequestServer.java:
package com.elusiva.rdp.applet;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.Map;
  
public class RequestServer {  
	public static void main(String[] args) throws IOException {
		String requestURL = "http://192.168.210.201/sm/webservices/server_random.php?type=windows&sid=1293712837Q5yTs";
		doRequest(requestURL);
	}

    public static Map<String,String> doRequest(String requestURL) {  
    	Map<String,String> params = null;
		try {
			// URL
			URL url = new URL(requestURL);
			
			// URL 
	        URLConnection connection = url.openConnection();  
	        
	        /*
	         *  。URLConnection , Web 。 
	         *  URLConnection , Web , : 
	         */  
	        connection.setDoOutput(true);  
	        
	        //  , :  
	        InputStream is = connection.getInputStream();  
	        
	        // XML 
	        params = XMLReader.ParseXML(is);
		} catch (MalformedURLException e) {
			System.out.println(" URL !");
		} catch(IOException e){
			System.out.println(" !");
		}
		
		return params;
    }  
}  

XMLReader.java:
package com.elusiva.rdp.applet;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class XMLReader {
	private final static Map<String,String> params = new HashMap<String,String>();
	public static Map<String,String> ParseXML(InputStream is) {
		try {
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse(is);
			// XML 
			doc.normalize(); 

			NodeList links = doc.getElementsByTagName("server");
			Element link = (Element) links.item(0);
			
			// map 
			String code = link.getElementsByTagName("code").item(0).getFirstChild().getNodeValue();
			if("0".equals(code)){
				params.put("ip", link.getElementsByTagName("ip").item(0)
						.getFirstChild().getNodeValue());
				params.put("loginName", link.getElementsByTagName("loginName").item(0)
						.getFirstChild().getNodeValue());
				params.put("loginPassword", link.getElementsByTagName("loginPassword").item(0)
						.getFirstChild().getNodeValue());
				params.put("appExePath", link.getElementsByTagName("appExePath").item(0)
						.getFirstChild().getNodeValue());
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
		
		return params;
	}
}