/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

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