/razpub/src/com/razie/pub/comms/MoreComms.java

http://razpub.googlecode.com/ · Java · 157 lines · 82 code · 18 blank · 57 comment · 4 complexity · 890aca9f39fc1e0f3d533e5cb7444b6d MD5 · raw file

  1. /**
  2. * Razvan's public code.
  3. * Copyright 2008 based on Apache license (share alike) see LICENSE.txt for details.
  4. */
  5. package com.razie.pub.comms;
  6. import java.io.BufferedReader;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.net.MalformedURLException;
  14. import java.net.Socket;
  15. import java.net.URL;
  16. import java.net.URLConnection;
  17. import java.util.List;
  18. import org.xml.sax.InputSource;
  19. import org.xml.sax.SAXException;
  20. import razie.assets.AssetLocation;
  21. import razie.base.AttrAccess;
  22. import com.razie.pub.base.data.ByteArray;
  23. import com.razie.pub.base.log.Log;
  24. import com.sun.org.apache.xerces.internal.parsers.DOMParser;
  25. /**
  26. * communications utils
  27. *
  28. * TODO detailed docs
  29. *
  30. * @author razvanc99
  31. *
  32. */
  33. public class MoreComms extends Comms {
  34. /**
  35. * Stream the response of a URL.
  36. *
  37. * @param url can be local or remote
  38. * @return a string containing the text read from the URL. can be the result of a servlet, a web
  39. * page or the contents of a local file. It's null if i couldn't read the file.
  40. */
  41. public static InputStream poststreamUrl(String url, AttrAccess httpArgs, String content) {
  42. try {
  43. InputStream in = null;
  44. // parse url to invoke sendPOST
  45. AssetLocation temploc = new AssetLocation (url);
  46. String cmd = "POST " + url.replace(temploc.toHttp(), "")+ " HTTP/1.1";
  47. Socket remote = HttpHelper.sendPOST(temploc.getHost(), Integer.decode(temploc.getPort()),
  48. cmd, httpArgs, content);
  49. in = remote.getInputStream();
  50. // TODO interpret the http response...error codes etc
  51. // if (!resCode.endsWith("200 OK")) {
  52. // String msg = "Could not fetch data from url " + url + ", resCode=" + resCode;
  53. // logger.trace(3, msg);
  54. // CommRtException rte = new CommRtException(msg);
  55. // if (uc.getContentType().endsWith("xml")) {
  56. // DOMParser parser = new DOMParser();
  57. // try {
  58. // parser.parse(new InputSource(in));
  59. // } catch (SAXException e) {
  60. // RuntimeException iex = new CommRtException("Error while processing document at "
  61. // + url);
  62. // iex.initCause(e);
  63. // throw iex;
  64. // }
  65. // }
  66. // throw rte;
  67. // }
  68. return in;
  69. } catch (MalformedURLException e) {
  70. throw new IllegalArgumentException(e);
  71. } catch (IOException e1) {
  72. // server/node down
  73. throw new RuntimeException("Connection exception for url="+url, e1);
  74. }
  75. }
  76. /**
  77. * Stream the response of a URL.
  78. *
  79. * @param url can be local or remote
  80. * @return a string containing the text read from the URL. can be the result of a servlet, a web
  81. * page or the contents of a local file. It's null if i couldn't read the file.
  82. */
  83. public static InputStream xpoststreamUrl2(String url, AttrAccess httpArgs, String content) {
  84. try {
  85. InputStream in = null;
  86. URLConnection uc = (new URL(url)).openConnection();
  87. // see http://www.exampledepot.com/egs/java.net/Post.html
  88. uc.setDoOutput(true);
  89. OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
  90. wr.write(content);
  91. wr.flush();
  92. logger.trace(3, "hdr: ", uc.getHeaderFields());
  93. String resCode = uc.getHeaderField(0);
  94. in = uc.getInputStream();
  95. if (!resCode.endsWith("200 OK")) {
  96. String msg = "Could not fetch data from url " + url + ", resCode=" + resCode;
  97. logger.trace(3, msg);
  98. RuntimeException rte = new RuntimeException(msg);
  99. if (uc.getContentType().endsWith("xml")) {
  100. DOMParser parser = new DOMParser();
  101. try {
  102. parser.parse(new InputSource(in));
  103. } catch (SAXException e) {
  104. RuntimeException iex = new RuntimeException("Error while processing document at "
  105. + url);
  106. iex.initCause(e);
  107. throw iex;
  108. }
  109. }
  110. throw rte;
  111. }
  112. return in;
  113. } catch (MalformedURLException e) {
  114. RuntimeException iex = new IllegalArgumentException();
  115. iex.initCause(e);
  116. throw iex;
  117. } catch (IOException e1) {
  118. // server/node down
  119. throw new RuntimeException("Connection exception for url="+url, e1);
  120. }
  121. }
  122. /**
  123. * Serialize to string the response of a URL, via POST rather than GET.
  124. *
  125. * @param url MUST be remotec
  126. * @param httpArgs the args to be sent with the HTTP request
  127. * @param content the content of the POST
  128. * @return a string containing the text read from the URL. can be the result of a servlet, a web
  129. * page or the contents of a local file. It's null if i couldn't read the file.
  130. */
  131. public static String readUrlPOST(String url, AttrAccess httpArgs, String content) {
  132. InputStream s = poststreamUrl(url, httpArgs, content);
  133. if (s == null) {
  134. return null;
  135. }
  136. return readStream(s);
  137. }
  138. static Log logger = Log.create(Comms.class.getName());
  139. }