📄 httpconnectionwrapper.java
字号:
/* License
*
* Copyright 1994-2005 Sun Microsystems, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistribution in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any kind. ALL
* EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
* ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
* AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
* REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
* INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
* OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
* EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or intended
* for use in the design, construction, operation or maintenance of any
* nuclear facility.
*/
package httpwrapper;
import java.io.*;
import javax.microedition.io.*;
import logging.*;
/**
* A wrapper for the HttpConnection interface that logs method
* calls and state transitions. Wrap a connection immediately
* after obtaining it from Connector.open. Information is logged
* according to the log level set the "httpwrapper" logger.
*/
public class HttpConnectionWrapper implements HttpConnection {
// Defines the logger used for logging everything.
public static final Logger logger = Logger.getLogger( "httpwrapper" );
static {
logger.setLevel( Level.FINE ); // Change this accordingly
}
public static final String SETUP_STATE = "setup";
public static final String CONNECTED_STATE = "connected";
public static final String CLOSED_STATE = "closed";
private static final String MSG_ALREADY_CLOSED =
"Connection has already been closed";
private static final String MSG_INPUT_STREAM =
"Input stream has already been opened";
private static final String MSG_OUTPUT_STREAM =
"Output stream has already been opened";
private static final String MSG_NOT_SETUP_STATE =
"Not in setup state";
private int _id;
private String _idString;
private HttpConnection _original;
private String _state;
private InputStreamWrapper _streamIn;
private OutputStreamWrapper _streamOut;
private static int _lastID;
/**
* Constructs a wrapper for an HttpConnection.
*/
public HttpConnectionWrapper( HttpConnection original ) {
_original = original;
_state = SETUP_STATE;
_id = ++_lastID;
_idString = generateID( _id );
info( "Wrapping HttpConnection " + original.getURL() );
info( "Entering setup state" );
}
/**
* Generates a new string ID for the wrapper.
*/
private static String generateID( int id ) {
StringBuffer b = new StringBuffer();
b.append( "[HTTP " );
b.append( id );
b.append( "] " );
return b.toString();
}
//--------------------------------------------------------------------------------------------
// Methods that can only be invoked in Setup state. Note that changing values doesn't
// have any effect once an output stream is open, so to catch these we throw an
// exception right away.
public void setRequestMethod(String str) throws IOException {
onlyInSetup( "setRequestMethod" );
if( _streamOut == null ){
info( "setRequestMethod( " + str + " )" );
try {
_original.setRequestMethod( str );
}
catch( IOException e ){
rethrow( "setRequestMethod", e );
}
} else {
throw makeException( "setRequestMethod", MSG_OUTPUT_STREAM );
}
}
public void setRequestProperty(String str, String str1) throws IOException {
onlyInSetup( "setRequestProperty" );
if( _streamOut == null ){
info( "setRequestProperty( " + str + ", " + str1 + " )" );
try {
_original.setRequestProperty( str, str1 );
}
catch( IOException e ){
rethrow( "setRequestProperty", e );
}
} else {
throw makeException( "setRequestProperty", MSG_OUTPUT_STREAM );
}
}
//--------------------------------------------------------------------------------------------
// Methods that cause transition to Connected state from Setup state
// and throw an error otherwise if closed
public InputStream openInputStream() throws IOException {
if( _streamIn != null ){
throw makeException( "openInputStream", MSG_INPUT_STREAM );
}
transitionToConnected( "openInputStream" );
info( "Opening input stream" );
try {
_streamIn = new InputStreamWrapper( _original.openInputStream() );
}
catch( IOException e ){
rethrow( "openInputStream", e );
}
return _streamIn;
}
public DataInputStream openDataInputStream() throws IOException {
return new DataInputStream( openInputStream() );
}
public long getLength() {
transitionToConnectedNoThrow( "getLength" );
long len = _original.getLength();
info( "getLength() = " + len );
return len;
}
public String getType() {
transitionToConnectedNoThrow( "getType" );
String type = _original.getType();
info( "getType() = " + type );
return type;
}
public String getEncoding() {
transitionToConnectedNoThrow( "getEncoding" );
String encoding = _original.getEncoding();
info( "getEncoding() = " + encoding );
return encoding;
}
public String getHeaderField(int param) throws IOException {
transitionToConnected( "getHeaderField" );
String field = null;
try {
field = _original.getHeaderField( param );
}
catch( IOException e ){
rethrow( "getHeaderField", e );
}
info( "getHeaderField( " + param + " ) = " + field );
return field;
}
public String getHeaderField(String str) throws IOException {
transitionToConnected( "getHeaderField" );
String field = null;
try {
field = _original.getHeaderField( str );
}
catch( IOException e ){
rethrow( "getHeaderField", e );
}
info( "getHeaderField( " + str + " ) = " + field );
return field;
}
public int getResponseCode() throws IOException {
transitionToConnected( "getResponseCode" );
int rc = 0;
try {
rc = _original.getResponseCode();
}
catch( IOException e ){
rethrow( "getResponseCode", e );
}
info( "getResponseCode() = " + rc );
return rc;
}
public String getResponseMessage() throws IOException {
transitionToConnected( "getResponseMessage" );
String msg = null;
try {
msg = _original.getResponseMessage();
}
catch( IOException e ){
rethrow( "getResponseMessage", e );
}
info( "getResponseMessage() = " + msg );
return msg;
}
public int getHeaderFieldInt(String str, int param) throws IOException {
transitionToConnected( "getHeaderFieldInt" );
int field = 0;
try {
field = _original.getHeaderFieldInt( str, param );
}
catch( IOException e ){
rethrow( "getHeaderFieldInt", e );
}
info( "getHeaderFieldInt( " + param + " ) = " + field );
return field;
}
public long getHeaderFieldDate(String str, long param) throws IOException {
transitionToConnected( "getHeaderFieldDate" );
long date = 0;
try {
date = _original.getHeaderFieldDate( str, param );
}
catch( IOException e ){
rethrow( "getHeaderFieldDate", e );
}
info( "getHeaderFieldDate( " + param + " ) = " + date );
return date;
}
public long getExpiration() throws IOException {
transitionToConnected( "getExpiration" );
long exp = 0;
try {
exp = _original.getExpiration();
}
catch( IOException e ){
rethrow( "getExpiration", e );
}
info( "getExpiration() = " + exp );
return exp;
}
public long getDate() throws IOException {
transitionToConnected( "getDate" );
long date = 0;
try {
date = _original.getDate();
}
catch( IOException e ){
rethrow( "getDate", e );
}
info( "getDate() = " + date );
return date;
}
public long getLastModified() throws IOException {
transitionToConnected( "getLastModified" );
long mod = 0;
try {
mod = _original.getLastModified();
}
catch( IOException e ){
rethrow( "getLastModified", e );
}
info( "getLastModified() = " + mod );
return mod;
}
public String getHeaderFieldKey(int param) throws IOException {
transitionToConnected( "getHeaderFieldKey" );
String key = null;
try {
key = _original.getHeaderFieldKey( param );
}
catch( IOException e ){
rethrow( "getHeaderFieldKey", e );
}
info( "getHeaderFieldKey( " + param + " ) = " + key );
return key;
}
//-------------------------------------------------------------------
// Methods that technically don't cause a transition to Connected
// until the stream is closed
public OutputStream openOutputStream() throws IOException {
if( _streamOut != null ){
throw makeException( "openOutputStream", MSG_OUTPUT_STREAM );
}
try {
_streamOut = new OutputStreamWrapper( _original.openOutputStream() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -