Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java Tip 103: Send HTTP requests for serialized objects

Implement Web object tunneling to transport Java objects through firewalls

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 2 of 4

  1:import java.net.URL;
  2:
  3:import jjv.net.HttpObjectChannel;
  4:import jjv.net.HttpObjectRequest;
  5:import jjv.net.HttpVarg;
  6:
  7:// JVHttpObjectChannelTester
  8:public class JVHttpObjectChannelTester implements Runnable {
  9:
 10:    /* constants */
 11:    public static final String    COMMAND_NAME  = new String("JVHttpObjectChannelTester");
 12:    public static final String    COMMAND_DESC  = new String("Web Tunneling Application Tester");
 13:    public static final String    COMMAND_VER   = new String("1.0");
 14:    public static final String    COMMAND_CR    = new String("Copyright (c) 2000 by JV - All Rights Reserved");
 15:
 16:    /* globals */
 17:    protected boolean  _trace;
 18:    protected String   _url;
 19:
 20:    // utilities
 21:    private void TRACE(String msg) { if (_trace) System.out.println("TRACE JVHttpObjectChannelTester " + msg); }
 22:    private void ERROR(String msg) { System.out.println("ERROR JVHttpObjectChannelTester " + msg); }
 23:
 24:    // Main
 25:    public static final void main(String args[]) { new JVHttpObjectChannelTester(args); }
 26:
 27:    // Constructor
 28:    public JVHttpObjectChannelTester(String args[]) {
 29:
 30:        /* init */
 31:        _trace = false;
 32:        _url = "http://host:port/servlets/MyChannelServlet";
 33:
 34:        /* try */
 35:        try {
 36:            int  i;
 37:
 38:            /* scan */
 39:            i = 0;
 40:            while (true) {
 41:              char  c;
 42:
 43:              /* set */
 44:              c = args[i].charAt(0);
 45:
 46:              /* check */
 47:              if ((c == '/') || (c == '-'))
 48:                c = args[i].charAt(1);
 49:
 50:              /* check */
 51:              switch(c) {
 52:                case 'v': /* version */
 53:                    System.out.println(COMMAND_NAME + " : " + COMMAND_DESC + " - Version : " + COMMAND_VER);
 54:                    System.out.println(COMMAND_CR);
 55:                    exit(0);
 56:
 57:                case 'T': /* trace */
 58:                    _trace = true;
 59:                    break;
 60:
 61:                case '?': /* bad option */
 62:                    this.usage();
 63:                    exit(1);
 64:              }
 65:
 66:              /* next */
 67:              i++;
 68:            }
 69:        } catch(ArrayIndexOutOfBoundsException excp) {;};
 70:
 71:        /* check */
 72:        if (this.init() < 0) {
 73:            ERROR("Cannot init...");
 74:            exit(1);
 75:        }
 76:
 77:        /* run */
 78:        this.run();
 79:    }
 80:
 81:    // Usage
 82:    public void usage() {
 83:        System.out.println("Usage: " + COMMAND_NAME + " [-v] [-T]");
 84:        System.out.println("Where:");
 85:        System.out.println("\t-v\tprint command version");
 86:        System.out.println("\t-T\ttrace");
 87:        return;
 88:    }
 89:
 90:    // Init
 91:    public int init() { return(0); }
 92:
 93:    // Run
 94:    public void run() {
 95:        String              url;
 96:        HttpObjectChannel   channel;
 97:        HttpObjectRequest   request;
 98:        HttpVarg            varg;
 99:        Object              response;
100:
101:        /* check */
102:        if ((url = _url) == null) {
103:            ERROR("run: Invalid URL, _url='" + _url + "'");
104:            usage();
105:            exit(1);
106:        }
107:
108:        /* try */
109:        try {
110:            channel = new HttpObjectChannel(this, new URL(url));
111:        } catch(Exception excp) {
112:            channel = null;
113:            ERROR("run: Exception caught while creating channel, excp='" + excp + "'");
114:            excp.printStackTrace();
115:            exit(1);
116:        }
117:
118:        /* create */
119:        request = channel.createRequest();
120:
121:        /* set */
122:        varg = new HttpVarg();
123:        varg.set("var1", "val1");
124:        varg.set("var2", "val2");
125:        varg.set("var3", "val3");
126:
127:        /* check */
128:        if ((response = request.exec(varg)) == null) {
129:            ERROR("run: Cannot exec request");
130:            exit(1);
131:        }
132:
133:        /* */
134:        TRACE("The Response is '" + response + "'");
135:
136:        /* exit */
137:        exit(0);
138:    }
139:
140:    // Exit
141:    public void exit(int code) { System.exit(code); }
142:}


First, you need to create a channel (line 110) between the client (this program) and the servlet (the URL). Then you can initiate an HTTP request (HttpObjectRequest) by calling channel.createRequest() (line 119). Some variables can be set inside an HttpVarg object, to be sent to the servlet and processed there (from line 122, where the HttpVarg object is created, to line 125). The request is performed at line 128 by calling the exec(varg) method. The response object is returned at the same time (line 128) and is finally printed by the client.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources