KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > 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 JavaDoc;
28 import java.io.BufferedOutputStream JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.Serializable JavaDoc;
33 import java.net.URLConnection JavaDoc;
34 import java.net.HttpURLConnection JavaDoc;
35 import java.net.URL JavaDoc;
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 JavaDoc UNKNOWN_HOST = "Unknown host : ";
53     static final String JavaDoc 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 JavaDoc UNAUTHORIZED_ACCESS =
55         "Invalid user or password";
56
57     private URLConnection JavaDoc mConnection = null;
58     private ObjectOutputStream JavaDoc mObjectOutStream = null;
59     private ObjectInputStream JavaDoc mObjectInStream = null;
60
61
62    ServletConnection(HttpConnectorAddress a) throws IOException JavaDoc{
63      try{
64        mConnection = a.openConnection("/"+AdminConstants.kAdminServletURI);
65      }
66      catch (IOException JavaDoc ioe){
67        handleException(ioe);
68      }
69    }
70
71
72     /**
73         Read an incoming Object.
74      */

75     public Object JavaDoc receive( ) throws IOException JavaDoc, ClassNotFoundException JavaDoc
76     {
77         Object JavaDoc value = null;
78         try
79         {
80             mObjectInStream = new ObjectInputStream JavaDoc(
81                 new BufferedInputStream JavaDoc(mConnection.getInputStream()));
82             value = mObjectInStream.readObject();
83         }
84         catch (IOException JavaDoc 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 JavaDoc object ) throws IOException JavaDoc
95     {
96         try
97         {
98             mObjectOutStream = new ObjectOutputStream JavaDoc(
99                                     new BufferedOutputStream JavaDoc(
100                                         mConnection.getOutputStream()));
101             mObjectOutStream.writeObject(object);
102             mObjectOutStream.flush();
103             mObjectOutStream.close();
104         }
105         catch (IOException JavaDoc 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 JavaDoc e)
119         {
120             Debug.printStackTrace(e);
121         }
122     }
123
124
125     private void handleException(IOException JavaDoc e) throws IOException JavaDoc
126     {
127         IOException JavaDoc exception = null;
128         if (e instanceof java.net.UnknownHostException JavaDoc)
129         {
130             exception = new java.net.UnknownHostException JavaDoc(UNKNOWN_HOST +
131                                                           e.getMessage());
132         }
133         else if (e instanceof java.net.ConnectException JavaDoc)
134         {
135             exception = new java.net.ConnectException JavaDoc(INVALID_HOST_PORT);
136         }
137         else
138         {
139             int responseCode =
140                 ((HttpURLConnection JavaDoc)mConnection).getResponseCode();
141             if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED)
142             {
143                 exception = new IOException JavaDoc(UNAUTHORIZED_ACCESS);
144             }
145             else
146             {
147                 exception = e;
148             }
149         }
150         throw exception;
151     }
152 }
153
Popular Tags