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