FileDocCategorySizeDatePackage
ServletConnection.javaAPI DocGlassfish v2 API10475Fri May 04 22:36:22 BST 2007com.sun.enterprise.admin.jmx.remote.comm

ServletConnection

public class ServletConnection extends Object implements Runnable, IConnection
A packager private Class (implementing {@link IConnection} communicating with Servlet over HTTP. Internally it uses java.net.URLConnection, for we may need to use it for both HTTP and HTTPS. In case of java.net.HttpURLConnection, only HTTP can be used. The server side (servlet etc) has to be configured as documented, for this connection to work.
author
Kedar Mhaswade
since
S1AS8.0
version
1.0

Fields Summary
static final String
UNKNOWN_HOST
static final String
UNAUTHORIZED_ACCESS
private URLConnection
mConnection
private ObjectOutputStream
mObjectOutStream
private ObjectInputStream
mObjectInStream
private HttpConnectorAddress
hca
private URL
uri
boolean
isRedirectionEnabled
private static final Logger
logger
private boolean
response_received
private int
streamSize
private int
reqSize
Constructors Summary
ServletConnection(HttpConnectorAddress a)

/* END -- S1WS_MOD */

        
        try{
            String uri = a.getPath();
            if (uri == null || uri.trim().length() == 0)
                uri = DefaultConfiguration.DEFAULT_SERVLET_CONTEXT_ROOT;
            mConnection = a.openConnection(uri);
            this.hca = a;
            String flag = System.getProperty(
                    DefaultConfiguration.REDIRECTION_DETECTION);
            if (flag != null && flag.equalsIgnoreCase("false"))
                isRedirectionEnabled = false;            
        } catch (IOException ioe){
            handleException(ioe);
        }
    
Methods Summary
public voidclose()

		try {
			mObjectInStream.close();
			mObjectOutStream.close();
		}
		catch(Exception e) {
			throw new RuntimeException (e);
		}
	
private java.lang.StringcreateConnectExceptionMessage()

            /* this is not internationalized yet -- this is because there is a 
               separate delivery of this code that cannot depend on other parts 
               of application server
            */
            final String h = hca.getHost();
            final int    p = hca.getPort();
            final String msg = "Unable to connect to admin-server at given host: [" + h + "] and port: [" + p + "].\nPlease check if this server is up and running and that the host and port provided are correct.";
            return ( msg );
        
HttpConnectorAddressgetHttpConnectorAddress()
Returns the URL to which Servlet Connection has been established

        return hca;
    
public java.net.URLgetURL()
Returns the URL to which Servlet Connection has been established

        return mConnection.getURL();
    
private voidhandleException(java.io.IOException e)

		IOException exception = null;
		if (e instanceof java.net.UnknownHostException) {
			exception = new java.net.UnknownHostException(UNKNOWN_HOST +
			e.getMessage());
		}
		else if (e instanceof java.net.ConnectException) {
			exception = new java.net.ConnectException(createConnectExceptionMessage());
		}
		else {
			int responseCode =
			((HttpURLConnection)mConnection).getResponseCode();
			if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
				exception = new IOException(UNAUTHORIZED_ACCESS);
			}
			else {
				exception = e;
			}
		}
		throw exception;
	
public java.lang.Objectreceive()
Read an incoming Object.

	Object value = null;
        try {            
            if (isRedirectionEnabled) {
                int respCode = ((HttpURLConnection)mConnection).getResponseCode();
                if (respCode == HttpURLConnection.HTTP_MOVED_TEMP) {
                    String redirectUrl = 
                        mConnection.getHeaderFields().get("Location").get(0);
                    throw new RedirectException(redirectUrl, 
                        "HTTP connection failed: 302 Moved Temporarily");
                }
            }
            InputStream in = mConnection.getInputStream();
            mObjectInStream = new ObjectInputStream(in);
            value = mObjectInStream.readObject();
            response_received = true;
            JMXInbandStream.setOutputStream(null, 0);
            StreamMBeanServerResponseMessage res = 
                (StreamMBeanServerResponseMessage) value;
            if (res.isStreamAvailable()) {
                JMXInbandStream.setIncomingStream(
                    new JMXChunkedInputStream(in));
            }           
        } catch (IOException ioe) {
            if (ioe instanceof RedirectException) throw ioe;
            else handleException(ioe);
        }
        return value;
    
public voidrun()

        OutputStream out = null;
        try {
            out = new JMXChunkedOutputStream(mConnection.getOutputStream());
            InputStream in   = JMXInbandStream.getOutgoingStream();
            byte[] bytes = new byte[8192];
            int len = 0;
            int prLen = 0;
            int flBytes = 0;
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            JMXInbandStream.setOutputStream(null, 0);
            out.flush();
            ((JMXChunkedOutputStream)out).writeEOF(reqSize);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    
public voidsend(java.io.Serializable req)
Write an object to the connection

        InputStream in = null;
        if (req instanceof StreamMBeanServerRequestMessage) {
            StreamMBeanServerRequestMessage reqm =
                    (StreamMBeanServerRequestMessage) req;
            JMXInbandStream.setIncomingStream(null);
            in = JMXInbandStream.getOutgoingStream();
            if (in != null) {
                reqm.setStreamAvailable(true);
                int len = (int) JMXInbandStream.getOutgoingStreamLength();

                ByteArrayOutputStream byO = new ByteArrayOutputStream();
                ObjectOutputStream oO = new ObjectOutputStream(byO);
                oO.writeObject(reqm);
                reqSize = byO.size();
                byO.reset();

                int chunks = (len/8192) + 2;
                streamSize = reqSize + len + (chunks * 4);
                ((HttpURLConnection)mConnection).setFixedLengthStreamingMode(streamSize);
                mConnection.setRequestProperty("Connection", "Close");
            }
        }
        sendReq(req);
        if (in != null)
            sendStream();
    
private voidsendReq(java.io.Serializable object)

        response_received = false;
		try {
			mObjectOutStream = new ObjectOutputStream(
			new BufferedOutputStream(mConnection.getOutputStream()));
			mObjectOutStream.writeObject(object);
			mObjectOutStream.flush();
			//mObjectOutStream.close();
		}
		catch (IOException ioe) {
                        handleException(ioe);
		}
	
private voidsendStream()

/*        Thread thr = new Thread(this);
        thr.start();
*/
        run();