View Javadoc

1   /*
2    * This file is part of NetPisteur.
3    * 
4    * Copyright 2001-2002 : Olivier CORNEC, David BOUANCHEAU, Loic LOPEZ,
5    *                       Gaetan BOUDARD, Mael LE LANNOU, Julien ROBINEAU
6    * 
7    * Copyright 2002-2003 : Olivier BRIENS, Simon DEZE, Florence FRIGOULT,
8    *                       Olivier JOURNEAULT, Francois MARTINIER, Damien RAUDE-MORVAN
9    * 
10   * Copyright 2004-2007 : Damien RAUDE-MORVAN
11   * 
12   * NetPisteur is free software; you can redistribute it and/or modify
13   * it under the terms of the GNU General Public License as published by
14   * the Free Software Foundation; either version 2 of the License, or
15   * (at your option) any later version.
16   *
17   * NetPisteur is distributed in the hope that it will be useful,
18   * but WITHOUT ANY WARRANTY; without even the implied warranty of
19   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20   * GNU General Public License for more details.
21   *
22   * You should have received a copy of the GNU General Public License
23   * along with NetPisteur; if not, write to the Free Software
24   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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  	// attributes
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  	// constructor
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  	} // -----------------------------------------ConnexionLocal()
81  
82  	// /////////////////////////////////////
83  	// accessors
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  	} // ----------------------------------------------getTheUrl()
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 	} // ----------------------------------------getMessageErreur()
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 	} // ----------------------------------------traitementErreur()
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 	} // ---------------------------------------------deconnection()
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 	} // ----------------------------------------------getConnecte()
142 
143 	// /////////////////////////////////////
144 	// operations
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 		// initialisation du message d'erreur
154 		this.messageErreur = null;
155 
156 		try {
157 
158 			// affectation de l'url
159 			this.connexionLocal = (this.url).openConnection();
160 
161 			// Simule un utilisateur pour les requetes
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 	} // ----------------------------------------------------connexion()
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 	} // --------------------------------------------getSourceByString()
230 
231 	/*
232 	 * Gets the ressource of an url using the file protocol This ressource is
233 	 * store in a stream
234 	 * 
235 	 * @return InputStreamReader: The ressource of the url
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 	} // --------------------------------------------getSourceByString()
257 } // ------------------------------------------------------------------------------fin
258 // ConnexionLocal
259