//GEN-BEGIN:Client /** * This file is generated. Please do not change */ package arrays; import java.util.*; import java.io.*; import javax.microedition.io.*; /** * */ public class WebToMobileClient { /** The URL of the servlet gateway */ private String serverURL; /** The session cookie of this client */ private String sessionCookie; /** * Empty array used for no-argument calls, and to represent the value "void" */ private final static Object[] _ = new Object[0]; /** * Constructs a new WebToMobileClient * and initializes the URL to the servlet gateway from a hard-coded value. */ public WebToMobileClient() { this("http://localhost:8080/WebApplicationMethods/servlet/test0911.arrays.WebToMobileServlet"); } /** * Constructs a new WebToMobileClient * and initializes the URL to the servlet gateway from the given value * * @param serverURL URL of the deployed servlet */ public WebToMobileClient(String serverURL) { this.serverURL = serverURL; } public int[] returnsIntArray(int[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 281 }; return (int[])invokeServer(24, params, paramIDs); } public int[] returnsIntArrayNoParameter() throws IOException { return (int[])invokeServer(19, _, null); } public float[] returnsFloatArrayNoParameter() throws IOException { return (float[])invokeServer(21, _, null); } public long[] returnsLongArray(long[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 15 }; return (long[])invokeServer(25, params, paramIDs); } public float[] returnsFloatArray(float[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 100 }; return (float[])invokeServer(26, params, paramIDs); } public boolean[] returnsBooleanArrayNoParameter() throws IOException { return (boolean[])invokeServer(23, _, null); } public double[] returnsDoubleArrayNoParameter() throws IOException { return (double[])invokeServer(22, _, null); } public String[] returnsStringArray(String[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 40 }; return (java.lang.String[])invokeServer(30, params, paramIDs); } public double[] returnsDoubleArray(double[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 24 }; return (double[])invokeServer(27, params, paramIDs); } public long[] returnsLongArrayNoParameter() throws IOException { return (long[])invokeServer(20, _, null); } public char[] returnsCharArray(char[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 174 }; return (char[])invokeServer(29, params, paramIDs); } public boolean[] returnsBooleanArray(boolean[] param) throws IOException { Object params[] = new Object[] { param }; int paramIDs[] = new int[] { 72 }; return (boolean[])invokeServer(28, params, paramIDs); } /** * This method performes a dynamic invocation on the server. It is generic in * order to reduce the code size. * *@param requestID The id of the server service (method) we wish to * invoke. *@param parameters The parameters that should be passed to the server * (type safety is not checked by this method!) *@param returnType Is used to indicate the return type we should read * from the server *@return The return value from the invoked service *@exception IOException When a communication error or a remove exception * occurs */ private Object invokeServer(int requestID, Object[] parameters, int[] paramIDs ) throws IOException { HttpConnection connection = (HttpConnection) Connector.open( serverURL ); connection.setRequestMethod(HttpConnection.POST); connection.setRequestProperty("Content-Type", "application/octet-stream"); connection.setRequestProperty("Accept", "application/octet-stream"); if (sessionCookie == null) { // if this is the first time this client contatcs the server, // verify that the version matches connection.setRequestProperty("version", "???"); } else { connection.setRequestProperty("cookie", sessionCookie); } DataOutputStream output = connection.openDataOutputStream(); // Write invocation code output.writeShort( 1 ); /* Write the byte signifying that only one call * is being made. */ // TODO This is not reflected on server now //output.writeShort(1 /* one call to be made to the server */); output.writeInt(requestID); for (int i = 0; i < parameters.length; i++ ) { writeObject(output, parameters[i], paramIDs[i]); } output.close(); int response; try { response = connection.getResponseCode(); } catch (IOException e) { throw new IOException("No response from " + serverURL); } if (response != 200) { throw new IOException(response + " " + connection.getResponseMessage()); } DataInputStream input = connection.openDataInputStream(); String sc = connection.getHeaderField("set-cookie"); if (sc != null) { sessionCookie = sc; } short errorCode = input.readShort(); if (errorCode != 1) { // there was a remote exception throw new IOException(input.readUTF()); } Object returnValue = readObject(input); input.close(); connection.close(); return returnValue; } /** * Serializes object to the stream with given type id * * @param out * @param o object to be serialized to the stream * @param id idetification code of the serialized object */ private void writeObject( DataOutputStream out, Object o, int id ) throws IOException { if( o == null ) { // write null type to the stream out.writeShort( -1 ); return; } Object[] array; switch( id ) { case 449: // int out.writeShort(449); out.writeInt(((Integer)o).intValue()); break; case 281: // int[] out.writeShort(281); int[] a_result_281 = (int[]) o; out.writeInt( a_result_281.length ); for( int i = 0; i < a_result_281.length; i++ ) { writeObject( out, new Integer(a_result_281[i]) , 449 ); } break; case 156: // long out.writeShort(156); out.writeLong(((Long)o).longValue()); break; case 15: // long[] out.writeShort(15); long[] a_result_15 = (long[]) o; out.writeInt( a_result_15.length ); for( int i = 0; i < a_result_15.length; i++ ) { writeObject( out, new Long(a_result_15[i]) , 156 ); } break; case 362: // double out.writeShort(362); out.writeDouble( ((Double)o).doubleValue() ); break; case 24: // double[] out.writeShort(24); double[] a_result_24 = (double[]) o; out.writeInt( a_result_24.length ); for( int i = 0; i < a_result_24.length; i++ ) { writeObject( out, new Double(a_result_24[i]) , 362 ); } break; case 82: // float out.writeShort(82); out.writeFloat( ((Float)o).floatValue() ); break; case 100: // float[] out.writeShort(100); float[] a_result_100 = (float[]) o; out.writeInt( a_result_100.length ); for( int i = 0; i < a_result_100.length; i++ ) { writeObject( out, new Float(a_result_100[i]) , 82 ); } break; case 414: // String out.writeShort(414); out.writeUTF((String)o); break; case 63: // char out.writeShort(63); out.writeChar(((Character)o).charValue()); break; case 174: // char[] out.writeShort(174); char[] a_result_174 = (char[]) o; out.writeInt( a_result_174.length ); for( int i = 0; i < a_result_174.length; i++ ) { writeObject( out, new Character(a_result_174[i]) , 63 ); } break; case 40: // String[] out.writeShort(40); java.lang.String[] a_result_40 = (java.lang.String[]) o; out.writeInt( a_result_40.length ); for( int i = 0; i < a_result_40.length; i++ ) { writeObject( out, a_result_40[i], 414 ); } break; case 72: // boolean[] out.writeShort(72); boolean[] a_result_72 = (boolean[]) o; out.writeInt( a_result_72.length ); for( int i = 0; i < a_result_72.length; i++ ) { writeObject( out, new Boolean(a_result_72[i]) , 31 ); } break; case 31: // boolean out.writeShort(31); out.writeBoolean(((Boolean)o).booleanValue()); break; default: // default if a data type is not supported throw new IllegalArgumentException("Unsupported parameter type: " + o.getClass()); } } private static Object readObject(DataInput in) throws IOException { int type = in.readShort(); int length; Object result; switch (type) { case 449: // int return new Integer(in.readInt()); case 281: // int[] int a_size_281 = in.readInt(); int[] a_result_281 = new int[ a_size_281 ]; for( int a_i_281 = 0; a_i_281 < a_size_281; a_i_281++ ) { a_result_281[a_i_281] = ((Integer)readObject( in )).intValue(); } result = a_result_281; return result; case 156: // long return new Long(in.readLong()); case 15: // long[] int a_size_15 = in.readInt(); long[] a_result_15 = new long[ a_size_15 ]; for( int a_i_15 = 0; a_i_15 < a_size_15; a_i_15++ ) { a_result_15[a_i_15] = ((Long)readObject( in )).longValue(); } result = a_result_15; return result; case 362: // double return new Double(in.readDouble()); case 24: // double[] int a_size_24 = in.readInt(); double[] a_result_24 = new double[ a_size_24 ]; for( int a_i_24 = 0; a_i_24 < a_size_24; a_i_24++ ) { a_result_24[a_i_24] = ((Double)readObject( in )).doubleValue(); } result = a_result_24; return result; case 82: // float return new Float(in.readFloat()); case 100: // float[] int a_size_100 = in.readInt(); float[] a_result_100 = new float[ a_size_100 ]; for( int a_i_100 = 0; a_i_100 < a_size_100; a_i_100++ ) { a_result_100[a_i_100] = ((Float)readObject( in )).floatValue(); } result = a_result_100; return result; case 414: // String result = in.readUTF(); return result; case 174: // char[] int a_size_174 = in.readInt(); char[] a_result_174 = new char[ a_size_174 ]; for( int a_i_174 = 0; a_i_174 < a_size_174; a_i_174++ ) { a_result_174[a_i_174] = ((Character)readObject( in )).charValue(); } result = a_result_174; return result; case 63: // char return new Character(in.readChar()); case 40: // String[] int a_size_40 = in.readInt(); java.lang.String[] a_result_40 = new java.lang.String[ a_size_40 ]; for( int a_i_40 = 0; a_i_40 < a_size_40; a_i_40++ ) { a_result_40[a_i_40] = readObject( in ); } result = a_result_40; return result; case 31: // boolean return new Boolean(in.readBoolean()); case 72: // boolean[] int a_size_72 = in.readInt(); boolean[] a_result_72 = new boolean[ a_size_72 ]; for( int a_i_72 = 0; a_i_72 < a_size_72; a_i_72++ ) { a_result_72[a_i_72] = ((Boolean)readObject( in )).booleanValue(); } result = a_result_72; return result; case -1: /* NULL */ return null; } throw new IllegalArgumentException("Unsupported return type (" + type + ")"); } } //GEN-END:Client