PageRenderTime 18ms CodeModel.GetById 8ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/razweb/src/com/razie/pub/http/LightCmdPOST.java

http://razpub.googlecode.com/
Java | 74 lines | 48 code | 10 blank | 16 comment | 8 complexity | 253a9a2e08fe2e94e8a3f30bff925198 MD5 | raw file
 1package com.razie.pub.http;
 2
 3import java.io.DataInputStream;
 4import java.io.ObjectInputStream;
 5import java.util.Properties;
 6
 7import com.razie.pub.base.data.HttpUtils;
 8import com.razie.pub.base.log.Log;
 9import com.razie.pub.comms.AuthException;
10import com.razie.pub.comms.MyServerSocket;
11
12/**
13 * POST is the same as GET but there are more attributes, there may be a content with mime type and no reply expected
14 * 
15 * <code>server.registerCmdListener(new LightCmdGET());</code>
16 * 
17 * @author razvanc
18 */
19public class LightCmdPOST extends LightCmdGET {
20   LightCmdGET delegateTo=null;
21   public LightCmdPOST (LightCmdGET delegateTo) {
22      this.delegateTo = delegateTo;
23   }
24   public Object execServer(String cmdName, String protocol, String args, Properties parms,
25         MyServerSocket socket) throws AuthException {
26      String input = "";
27
28      try {
29         // special hacking for binary transmissions
30         if (socket.getHttp() != null && socket.getHttp().isPopulated("Content-Type")
31               && socket.getHttp().sa("Content-Type").equals("application/octet-stream")) {
32            // only thing i support right now is an object...
33
34            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
35            Object bee = ois.readObject();
36            parms.put("razie.content.mime.octet-stream", bee);
37         } else {
38            // POST has parms after all the http stuff already read by server. read them like this
39            // since there's no CRLF
40
41            // TODO should use the content-length so i don't miss input in large requests, maybe
42
43            // BufferedReader in = new BufferedReader(new
44            // InputStreamReader(socket.getInputStream()));
45            DataInputStream in = new DataInputStream(socket.getInputStream());
46            if (in.available() > 0) {
47               // TODO 3-2 i only accept an url of 1k chars - is that fair?
48               byte[] cbuf = new byte[1000];
49               int c = in.read(cbuf);
50               input = new String(cbuf, 0, c);
51               Log.traceThis("POST INPUT=" + input);
52            }
53            // decode the parms
54            String[] pairs = input.split("&");
55            for (String pair : pairs) {
56               String[] split = pair.split("=", 2);
57               if (split.length > 1)
58                  parms.put(split[0], HttpUtils.fromUrlEncodedString(split[1]));
59            }
60         }
61      } catch (Exception e) {
62         Log.logThis("ERROR when decoding a POST request...", e);
63      }
64
65      return delegateTo.execServer(cmdName, protocol, args, parms, socket);
66   }
67
68   public String[] getSupportedActions() {
69      return COMMANDS;
70   }
71
72   static final String[] COMMANDS = { "POST" };
73   static final Log      logger   = Log.create("", LightCmdPOST.class.getName());
74}