Project

General

Profile

1
/**
2
 *        Name: HttpMessage.java
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
 *
10
 *     Version: '$Id: HttpMessage.java 647 2001-01-09 18:57:58Z berkley $'
11
 *
12
 *     '$Author: berkley $'
13
 *     '$Date: 2001-01-09 10:57:58 -0800 (Tue, 09 Jan 2001) $'
14
 *     '$Revision: 647 $'
15
 */
16

    
17
package edu.ucsb.nceas.metacat;
18

    
19
import java.io.*;
20
import java.net.*;
21
import java.util.*;
22

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

    
117
    /**
118
     * Returns the urls argument strings
119
     */
120
    public String getArgString() {
121
        String argString1 = argString;
122
        if (!argString1.startsWith("?")) {
123
            argString1 = "?"+argString1;}
124
        return argString1;
125
    }
126
    
127
    /**
128
     * Converts a Properties list to a URL-encoded query string
129
     */
130
    private String toEncodedString(Properties args) {
131
        StringBuffer buf = new StringBuffer();
132
        Enumeration names = args.propertyNames();
133
        while (names.hasMoreElements()) {
134
            String name = (String)names.nextElement();
135
            String value = args.getProperty(name);
136
            buf.append(URLEncoder.encode(name) + "=" + URLEncoder.encode(value));
137
            if (names.hasMoreElements()) buf.append("&");
138
        }
139
        return buf.toString();
140
    }
141
}
142

    
(28-28/42)