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

/gwtrpccommlayer/src/main/java/com/googlecode/gwtrpccommlayer/client/internal/factory/impl/PostFactoryImpl.java

https://code.google.com/p/gwtrpccommlayer/
Java | 114 lines | 80 code | 14 blank | 20 comment | 2 complexity | 214ecc1db5e9291f2fd98e44b62fe113 MD5 | raw file
  1package com.googlecode.gwtrpccommlayer.client.internal.factory.impl;
  2
  3import com.googlecode.gwtrpccommlayer.client.internal.factory.PostFactory;
  4import com.googlecode.gwtrpccommlayer.shared.GwtRpcCommLayerPojoConstants;
  5import com.googlecode.gwtrpccommlayer.shared.GwtRpcCommLayerPojoRequest;
  6import com.googlecode.gwtrpccommlayer.shared.GwtRpcCommLayerPojoResponse;
  7import org.apache.http.HttpEntity;
  8import org.apache.http.HttpResponse;
  9import org.apache.http.client.methods.HttpPost;
 10import org.apache.http.entity.InputStreamEntity;
 11
 12import java.io.*;
 13import java.net.URISyntaxException;
 14import java.net.URL;
 15
 16/**
 17 * Created by IntelliJ IDEA.
 18 * User: dan
 19 * Date: 10/31/10
 20 * Time: 9:31 PM
 21 */
 22public class PostFactoryImpl implements PostFactory{
 23    @Override
 24    public HttpPost create(URL url, GwtRpcCommLayerPojoRequest request) {
 25        /*
 26        * SERIALZED THE POJO-REQUEST OBJECT INTO BYTES
 27        */
 28        try {
 29            byte[] pojoByteArray = serializeIntoBytes(request);
 30            long length = pojoByteArray.length;
 31            ByteArrayInputStream in = new ByteArrayInputStream(pojoByteArray);
 32            InputStreamEntity reqEntity = new InputStreamEntity(in, length);
 33            reqEntity.setContentType("binary/octet-stream");
 34            reqEntity.setChunked(false);
 35
 36
 37            /*
 38            * CONSTRUCT THE URL
 39            */
 40            //String url = createFullyQualifiedURL();
 41
 42            /*
 43            * Create POST instance
 44            */
 45            //HttpPost httppost = new HttpPost(url);
 46            HttpPost httppost = null;
 47            httppost = new HttpPost(url.toURI());
 48            httppost.setEntity(reqEntity);
 49
 50            /*
 51            * Add the correct user-agent
 52            */
 53            httppost.addHeader(GwtRpcCommLayerPojoConstants.GWT_RPC_COMM_LAYER_CLIENT_KEY, GwtRpcCommLayerPojoConstants.GWT_RPC_COMM_LAYER_POJO_CLIENT);
 54
 55            return httppost;
 56        } catch (URISyntaxException e) {
 57            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 58        } catch (IOException e) {
 59            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 60        }
 61
 62        return null;
 63    }
 64
 65    @Override
 66    public GwtRpcCommLayerPojoResponse create(HttpResponse response) {
 67        HttpEntity resEntity = response.getEntity();
 68        byte[] respData = new byte[0];
 69        try {
 70            respData = deserializeIntoBytes(resEntity);
 71            GwtRpcCommLayerPojoResponse pojoResp = createInstanceFromBytes(respData);
 72            return pojoResp;
 73        } catch (IOException e) {
 74            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 75        } catch (ClassNotFoundException e) {
 76            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 77        }
 78        return null;
 79    }
 80    private GwtRpcCommLayerPojoResponse createInstanceFromBytes(byte[] data) throws ClassNotFoundException, IOException {
 81        ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(data));
 82        Object instance = objIn.readObject();
 83        GwtRpcCommLayerPojoResponse pojoResp = (GwtRpcCommLayerPojoResponse) instance;
 84
 85        return pojoResp;
 86    }
 87
 88    private byte[] serializeIntoBytes(GwtRpcCommLayerPojoRequest request) throws IOException {
 89        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 90        ObjectOutputStream objOut = new ObjectOutputStream(bos);
 91        objOut.writeObject(request);
 92        objOut.flush();
 93        objOut.close();
 94
 95        byte[] all = bos.toByteArray();
 96        return all;
 97    }
 98
 99    private byte[] deserializeIntoBytes(HttpEntity respEntity) throws IOException {
100        byte[] b = new byte[512];
101        ByteArrayOutputStream buff = new ByteArrayOutputStream();
102        InputStream respIn = respEntity.getContent();
103        while(true)
104        {
105            int vv = respIn.read(b);
106            if ( vv == -1 )
107            {
108                break;
109            }
110            buff.write(b, 0, vv);
111        }
112        return buff.toByteArray();
113    }
114}