1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package de.bea.domingo.server;
24
25 import java.io.IOException;
26 import java.io.PrintWriter;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.StringTokenizer;
30
31 import de.bea.domingo.DDocument;
32 import de.bea.domingo.DNotesException;
33 import de.bea.domingo.notes.DAgentBase;
34
35 /***
36 * Adapter from a domingo agent to the domingo server.
37 *
38 * @author <a href=mailto:kriede@users.sourceforge.net>Kurt Riede</a>
39 */
40 public final class DomingoServerAgent extends DAgentBase {
41
42 /*** ASCII code of the plus sign (+). */
43 private static final int CHAR_PLUS = 43;
44
45 /*** ASCII code of the percentage sign (%). */
46 private static final int CHAR_PERCENT = 37;
47
48 /*** Radix for hexa-decimal representations of numbers (16). */
49 private static final int RADIX_16 = 16;
50
51 /***
52 * @see de.bea.domingo.notes.DAgentBase#main()
53 */
54 public void main() {
55 final DDocument doc = getDSession().getAgentContext().getDocumentContext();
56 final Map parameters = parseQueryString(doc.getItemValueString("QUERY_STRING"));
57 final DomingoServer server = new DomingoServer();
58 final PrintWriter agentOutput = getAgentOutput();
59 try {
60 server.execute(getDSession(), parameters, agentOutput);
61 } catch (UnsupportedOperationException e) {
62 e.printStackTrace();
63 } catch (DNotesException e) {
64 e.printStackTrace();
65 } catch (IOException e) {
66 e.printStackTrace();
67 }
68 }
69
70 private Map parseQueryString(final String s) {
71 if (s == null) {
72 throw new IllegalArgumentException();
73 }
74 Map map = new HashMap();
75 StringBuffer stringbuffer = new StringBuffer();
76 String name;
77 String[] values;
78 StringTokenizer stringtokenizer = new StringTokenizer(s, "&");
79 while (stringtokenizer.hasMoreTokens()) {
80 String token = stringtokenizer.nextToken();
81 int i = token.indexOf('=');
82 String value;
83 if (i == -1) {
84 name = parseName(token, stringbuffer);
85 value = null;
86 } else {
87 name = parseName(token.substring(0, i), stringbuffer);
88 value = parseName(token.substring(i + 1, token.length()), stringbuffer);
89 }
90 if (map.containsKey(name)) {
91 String[] as1 = (String[]) map.get(name);
92 values = new String[as1.length + 1];
93 for (int j = 0; j < as1.length; j++) {
94 values[j] = as1[j];
95 }
96 values[as1.length] = value;
97 } else {
98 values = new String[1];
99 values[0] = value;
100 }
101 map.put(name, values);
102 }
103 return map;
104 }
105
106 private String parseName(final String s, final StringBuffer stringbuffer) {
107 stringbuffer.setLength(0);
108 int i = 0;
109 while (i < s.length()) {
110 char c = s.charAt(i);
111 switch (c) {
112 case CHAR_PLUS:
113 stringbuffer.append(' ');
114 break;
115 case CHAR_PERCENT:
116 try {
117 stringbuffer.append((char) Integer.parseInt(s.substring(i + 1, i + 3), RADIX_16));
118 i += 2;
119 break;
120 } catch (NumberFormatException e) {
121 throw new IllegalArgumentException();
122 } catch (StringIndexOutOfBoundsException e) {
123 String s1 = s.substring(i);
124 stringbuffer.append(s1);
125 if (s1.length() == 2) {
126 i++;
127 }
128 }
129 break;
130 default:
131 stringbuffer.append(c);
132 break;
133 }
134 i++;
135 }
136 return stringbuffer.toString();
137 }
138 }