Java Source Code: com.sun.enterprise.admin.comm.ServletConnection


   1: /*
   2:  * The contents of this file are subject to the terms 
   3:  * of the Common Development and Distribution License 
   4:  * (the License).  You may not use this file except in
   5:  * compliance with the License.
   6:  * 
   7:  * You can obtain a copy of the license at 
   8:  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
   9:  * glassfish/bootstrap/legal/CDDLv1.0.txt.
  10:  * See the License for the specific language governing 
  11:  * permissions and limitations under the License.
  12:  * 
  13:  * When distributing Covered Code, include this CDDL 
  14:  * Header Notice in each file and include the License file 
  15:  * at glassfish/bootstrap/legal/CDDLv1.0.txt.  
  16:  * If applicable, add the following below the CDDL Header, 
  17:  * with the fields enclosed by brackets [] replaced by
  18:  * you own identifying information: 
  19:  * "Portions Copyrighted [year] [name of copyright owner]"
  20:  * 
  21:  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  22:  */
  23: 
  24: package com.sun.enterprise.admin.comm;
  25: 
  26: //jdk imports
  27: import java.io.BufferedInputStream;
  28: import java.io.BufferedOutputStream;
  29: import java.io.ObjectInputStream;
  30: import java.io.ObjectOutputStream;
  31: import java.io.IOException;
  32: import java.io.Serializable;
  33: import java.net.URLConnection;
  34: import java.net.HttpURLConnection;
  35: import java.net.URL;
  36: 
  37: //admin imports
  38: import com.sun.enterprise.admin.common.*;
  39: import com.sun.enterprise.admin.common.constant.AdminConstants;
  40: import com.sun.enterprise.admin.util.*;
  41: 
  42: /**
  43:     Class communicating with Servlet over HTTP. Internally it uses java.net.URLConnection,
  44:     for we may need to use it for both HTTP and HTTPS. In case of
  45:     java.net.HttpURLConnection, only HTTP can be used.
  46: 
  47:     @author Kedar Mhaswade
  48:     @version 1.0
  49: */
  50:  class ServletConnection implements IConnection
  51:	  {
  52:    static final String         UNKNOWN_HOST            = "Unknown host : ";
  53:    static final String         INVALID_HOST_PORT       = "Unable to connect to admin-server.  Please check if the server is up and running and that the host and port provided are correct.";
  54:    static final String         UNAUTHORIZED_ACCESS     = 
  55:        "Invalid user or password";
  56:
  57:    private URLConnection        mConnection           = null;
  58:    private ObjectOutputStream   mObjectOutStream      = null;
  59:    private ObjectInputStream    mObjectInStream       = null;
  60:
  61:
  62:	     ServletConnection(HttpConnectorAddress a) throws IOException{
  63:	       try{
  64:       mConnection = a.openConnection("/"+AdminConstants.kAdminServletURI);
  65:     }
  66:	       catch (IOException ioe){
  67:       handleException(ioe);
  68:     }
  69:   }
  70:
  71:
  72:    /**
  73:        Read an incoming Object.
  74:     */
  75:    public  Object receive(  ) throws IOException, ClassNotFoundException
  76:	      {
  77:        Object value = null;
  78:        try
  79:	          {
  80:            mObjectInStream = new ObjectInputStream(
  81:                new BufferedInputStream(mConnection.getInputStream()));
  82:            value = mObjectInStream.readObject();
  83:        }
  84:        catch (IOException ioe)
  85:	          {
  86:            handleException(ioe);
  87:        }
  88:        return value;
  89:    }
  90:
  91:    /**
  92:        Write an object to the connection
  93:     */
  94:    public  void    send( Serializable object ) throws IOException
  95:	      {
  96:        try
  97:	          {
  98:            mObjectOutStream = new ObjectOutputStream(
  99:                                    new BufferedOutputStream(
 100:                                        mConnection.getOutputStream()));
 101:            mObjectOutStream.writeObject(object);
 102:            mObjectOutStream.flush();
 103:            mObjectOutStream.close();
 104:        }
 105:        catch (IOException ioe)
 106:	          {
 107:            handleException(ioe);
 108:        }
 109:    }
 110:
 111:    public void    close()
 112:	      {
 113:        try
 114:	          {
 115:            mObjectInStream.close();
 116:            mObjectOutStream.close();
 117:        }
 118:        catch(Exception e)
 119:	          {
 120:            Debug.printStackTrace(e);
 121:        }
 122:    }
 123:
 124:
 125:    private void handleException(IOException e) throws IOException
 126:	      {
 127:        IOException exception = null;
 128:        if (e instanceof UnknownHostException)
 129:	          {
 130:            exception = new java.net.UnknownHostException(UNKNOWN_HOST + 
 131:                                                          e.getMessage());
 132:        }
 133:        else if (e instanceof ConnectException)
 134:	          {
 135:            exception = new java.net.ConnectException(INVALID_HOST_PORT);
 136:        }
 137:        else
 138:	          {
 139:            int responseCode = 
 140:                ((HttpURLConnection)mConnection).getResponseCode();
 141:            if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED)
 142:	              {
 143:                exception = new IOException(UNAUTHORIZED_ACCESS);
 144:            }
 145:            else
 146:	              {
 147:                exception = e;
 148:            }
 149:        }
 150:        throw exception;
 151:    }
 152:}