Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: Used for Java applet/application communication
4
 *             with servlet. Based on code given in the book
5
 *             "Java Servlet Programming" by Hunter & crawford
6
 *  Copyright: 2000 Regents of the University of California and the
7
 *             National Center for Ecological Analysis and Synthesis
8
 *    Authors: Dan Higgins modified for metacat by Chad Berkley
9
 *    Release: @release@
10
 *
11
 *   '$Author: jones $'
12
 *     '$Date: 2001-01-18 11:52:00 -0800 (Thu, 18 Jan 2001) $'
13
 * '$Revision: 669 $'
14
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 */
29

    
30
package edu.ucsb.nceas.metacat;
31

    
32
import java.io.*;
33
import java.net.*;
34
import java.util.*;
35

    
36
/**
37
 * Send a message to an HTTP server using either a GET or POST operation
38
 */
39
public class HttpMessage {
40
    public String contype;
41
    URL servlet = null;
42
    String argString = null;
43
    static String cookie = null;
44
    public HttpMessage(URL servlet) {
45
        this.servlet = servlet;
46
    }
47
    
48
    /**
49
     * return the cookie that this message object contains
50
     */
51
    public static String getCookie()
52
    {
53
      return cookie;
54
    }
55
    
56
    /**
57
     * Performs a GET request to the previously given servlet
58
     * with no query string
59
    */
60
    public InputStream sendGetMessage() throws IOException {
61
        return sendGetMessage(null);
62
    }
63
    
64
    /**
65
     * Performs a GET request to the previously given servlet
66
     *Builds a query string from the supplied Properties list.
67
     */
68
    public InputStream sendGetMessage(Properties args) throws IOException {
69
        argString = ""; //default
70
        
71
        if (args != null) {
72
            argString = "?" + toEncodedString(args);
73
        }
74
        URL url = new URL(servlet.toExternalForm() + argString);
75
        
76
        // turn off caching
77
        URLConnection con = url.openConnection();
78
        con.setUseCaches(false);
79
        contype = con.getContentType();
80
        
81
        return con.getInputStream();
82
    }
83
    
84
    /**
85
     * Performs a POST request to the previously given servlet
86
     * with no query string
87
     */
88
    public InputStream sendPostMessage() throws IOException {
89
        return sendPostMessage(null);
90
    }
91
    
92
    /**
93
     * Builds post data from the supplied properties list
94
     */
95
    public InputStream sendPostMessage(Properties args) throws IOException {
96
        argString = ""; //default
97
        if (args != null) {
98
            argString = toEncodedString(args); 
99
        }
100
        URLConnection con = servlet.openConnection();
101
        if (cookie!=null) {
102
            int k = cookie.indexOf(";");
103
            if (k>0) {
104
                cookie = cookie.substring(0, k);
105
            }
106
            //System.out.println("Cookie = " + cookie);
107
            con.setRequestProperty("Cookie", cookie); 
108
            con.setRequestProperty("User-Agent", "MORPHO");  // add 10/26/00 by DFH so Metacat can determine where request come from
109
        }
110
        //prepare for both input and output
111
        con.setDoInput(true);
112
        con.setDoOutput(true);
113
        //turn off caching
114
        con.setUseCaches(false);
115
        
116
        //work around a Netscape bug
117
        //con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
118
        //Write the arguments as post data
119
        DataOutputStream out = new DataOutputStream(con.getOutputStream());
120
        out.writeBytes(argString);
121
        out.flush();
122
        contype = con.getContentType();
123
        String temp = con.getHeaderField("Set-Cookie");
124
        if (temp!=null) {
125
            cookie = temp;
126
        }
127
        //System.out.println(cookie);
128
        out.close();
129
        
130
        return con.getInputStream();
131
    }
132

    
133
    /**
134
     * Returns the urls argument strings
135
     */
136
    public String getArgString() {
137
        String argString1 = argString;
138
        if (!argString1.startsWith("?")) {
139
            argString1 = "?"+argString1;}
140
        return argString1;
141
    }
142
    
143
    /**
144
     * Converts a Properties list to a URL-encoded query string
145
     */
146
    private String toEncodedString(Properties args) {
147
        StringBuffer buf = new StringBuffer();
148
        Enumeration names = args.propertyNames();
149
        while (names.hasMoreElements()) {
150
            String name = (String)names.nextElement();
151
            String value = args.getProperty(name);
152
            buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
153
            if (names.hasMoreElements()) buf.append("&");
154
        }
155
        return buf.toString();
156
    }
157
}
(29-29/43)