/test/src/solidstack/httpclient/RequestWriter.java

http://solidstack.googlecode.com/ · Java · 156 lines · 137 code · 17 blank · 2 comment · 7 complexity · 252345fdaac8d711a88a5bce7b91581b MD5 · raw file

  1. package solidstack.httpclient;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.io.UnsupportedEncodingException;
  6. import java.io.Writer;
  7. import solidstack.httpserver.HttpException;
  8. public class RequestWriter extends Writer
  9. {
  10. protected OutputStreamWriter writer;
  11. protected String contentType;
  12. public RequestWriter( OutputStream out, String charsetName, String contentType )
  13. {
  14. super( out /* = lock object */ );
  15. this.contentType = contentType;
  16. try
  17. {
  18. this.writer = new OutputStreamWriter( out, charsetName );
  19. }
  20. catch( UnsupportedEncodingException e )
  21. {
  22. throw new HttpException( e );
  23. }
  24. }
  25. public RequestWriter( OutputStream out, String charsetName )
  26. {
  27. this( out, charsetName, null );
  28. }
  29. @Override
  30. public void write( char[] cbuf, int off, int len )
  31. {
  32. try
  33. {
  34. this.writer.write( cbuf, off, len );
  35. }
  36. catch( IOException e )
  37. {
  38. throw new HttpException( e );
  39. }
  40. }
  41. @Override
  42. public void write( int c )
  43. {
  44. try
  45. {
  46. this.writer.write( c );
  47. }
  48. catch( IOException e )
  49. {
  50. throw new HttpException( e );
  51. }
  52. }
  53. @Override
  54. public void write( char[] cbuf )
  55. {
  56. try
  57. {
  58. this.writer.write( cbuf );
  59. }
  60. catch( IOException e )
  61. {
  62. throw new HttpException( e );
  63. }
  64. }
  65. @Override
  66. public void write( String str )
  67. {
  68. try
  69. {
  70. this.writer.write( str );
  71. }
  72. catch( IOException e )
  73. {
  74. throw new HttpException( e );
  75. }
  76. }
  77. @Override
  78. public void write( String str, int off, int len )
  79. {
  80. try
  81. {
  82. this.writer.write( str, off, len );
  83. }
  84. catch( IOException e )
  85. {
  86. throw new HttpException( e );
  87. }
  88. }
  89. public void writeEncoded( String str )
  90. {
  91. if( this.contentType != null && this.contentType.equals( "text/html" ) )
  92. {
  93. for( char ch : str.toCharArray() )
  94. {
  95. switch( ch )
  96. {
  97. case '<': write( "&lt;" ); break;
  98. case '>': write( "&gt;" ); break;
  99. case '&': write( "&amp;" ); break;
  100. default: write( ch );
  101. }
  102. }
  103. }
  104. else
  105. write( str );
  106. }
  107. public void writeEncoded( Object o )
  108. {
  109. if( o != null )
  110. writeEncoded( o.toString() );
  111. }
  112. public void write( Object o )
  113. {
  114. write( o != null ? o.toString() : "null" );
  115. }
  116. @Override
  117. public void flush()
  118. {
  119. try
  120. {
  121. this.writer.flush();
  122. }
  123. catch( IOException e )
  124. {
  125. throw new HttpException( e );
  126. }
  127. // The reponse writer does not need to flush.
  128. // We don't want to trigger the flushing of the output stream.
  129. }
  130. @Override
  131. public void close()
  132. {
  133. throw new UnsupportedOperationException();
  134. }
  135. public String getEncoding()
  136. {
  137. return this.writer.getEncoding();
  138. }
  139. }