1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package com.drazzib.netpisteur.loader;
27
28 import java.io.BufferedReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.io.InputStreamReader;
32 import java.net.URL;
33 import java.net.URLConnection;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39 * <P>
40 * <STRONG>Description </STRONG>
41 * <P>
42 * Manage the connection local and get the content of the Url
43 *
44 * @see com.drazzib.netpisteur.loader.Connexion
45 * @version 1.0
46 * @since 2002/03/18
47 * @author NetPisteurTeam
48 */
49
50 public class ConnexionLocal extends Connexion {
51
52 private static Log log = LogFactory.getLog(ConnexionLocal.class);
53
54
55
56
57 /**
58 * Represents the local connection
59 */
60 private URLConnection connexionLocal;
61
62 private String messageErreur;
63
64 private boolean connecte;
65
66
67
68
69 /**
70 * Creates a ConnexionLocal object using the class UrlConnection
71 *
72 * @param uneUrl
73 * URL
74 */
75 public ConnexionLocal(URL uneUrl) {
76 super(uneUrl);
77 this.connexionLocal = null;
78 this.messageErreur = null;
79 this.connecte = false;
80 }
81
82
83
84
85 /**
86 * Gets the url of the connection
87 *
88 * @return URL : Url of the connection
89 */
90 public URL getTheUrl() {
91 if (this.connecte) {
92 return this.url;
93
94 }
95 return null;
96
97 }
98
99 /**
100 * Gets the message error of the connection
101 *
102 * @return String : message error of the connection
103 */
104 public String getMessageErreur() {
105 return this.messageErreur;
106
107 }
108
109 /**
110 * Checks errors and gets the message error of the connection
111 *
112 * @param unTypeErreur
113 * code error representing state of the connection
114 * @return String : message error od a code error
115 */
116 public String traitementErreur(int unTypeErreur) {
117 return this.messageErreur;
118
119 }
120
121 /**
122 * Manages the deconnection
123 *
124 * @return boolean : state of the connection
125 */
126
127 public boolean deconnexion() {
128 this.connecte = false;
129 return this.connecte;
130
131 }
132
133 /**
134 * Is the connection made ?
135 *
136 * @return boolean: return false if the connection to this url is impossible
137 */
138 public boolean getConnecte() {
139 return (this.connecte);
140
141 }
142
143
144
145
146 /**
147 * Opens a connection using a url and checking the error
148 *
149 * @return boolean: state of the connection
150 */
151
152 public boolean connexion() {
153
154 this.messageErreur = null;
155
156 try {
157
158
159 this.connexionLocal = (this.url).openConnection();
160
161
162 this.connexionLocal.setRequestProperty("User-Agent",
163 "Netpisteur v2");
164
165 this.connexionLocal.connect();
166
167 this.messageErreur = "Connexion URL locale russie. ";
168 this.connecte = true;
169 } catch (java.io.IOException exWithOpenConnection) {
170 this.messageErreur = "Connexion a l'url : " + getURL()
171 + " impossible. ";
172 }
173
174 return this.connecte;
175 }
176
177 /**
178 * Gets the ressource of an url using the file protocol This ressource is
179 * store in a string
180 *
181 * @return String: The ressource of the url
182 */
183
184 public String getSourceByString() {
185
186 InputStream fluxEntree = null;
187 InputStreamReader lecteurFlux = null;
188 BufferedReader netIn = null;
189 String reponseServeur = null;
190 String reponseServeurTmp = null;
191
192 try {
193 if (this.connecte) {
194
195 fluxEntree = this.connexionLocal.getInputStream();
196 lecteurFlux = new InputStreamReader(fluxEntree);
197 netIn = new BufferedReader(lecteurFlux);
198 do {
199 reponseServeurTmp = (netIn.readLine());
200 reponseServeur = reponseServeur + reponseServeurTmp;
201 } while (reponseServeurTmp != null);
202
203 } else {
204 reponseServeur = (this.messageErreur);
205
206 }
207
208 } catch (java.io.IOException exWithGetInputStream) {
209 this.messageErreur = "erreur de lecture de flux d'entree dans ConnexionHttp.getSourceByString()";
210 } finally {
211 try {
212 if (netIn != null)
213 {
214 netIn.close();
215 }
216 if (lecteurFlux != null) {
217 lecteurFlux.close();
218 }
219 if (fluxEntree != null) {
220 fluxEntree.close();
221 }
222 } catch (IOException e) {
223 log.error(e);
224 }
225 }
226
227 return reponseServeur;
228
229 }
230
231
232
233
234
235
236
237
238 public InputStreamReader getSourceByReader() {
239
240 InputStreamReader fluxEntree = null;
241
242 try {
243 if (this.connecte) {
244
245 fluxEntree = new InputStreamReader(this.connexionLocal
246 .getInputStream());
247 fluxEntree.close();
248 }
249
250 } catch (java.io.IOException exWithGetInputStream) {
251 this.messageErreur = "erreur de lecture de flux d'entree dans ConnexionHttp.getSourceByStream()";
252 }
253
254 return fluxEntree;
255
256 }
257 }
258
259