/*     */ package n38sim.net;
/*     */ 
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.net.URL;
/*     */ import java.net.URLConnection;
/*     */ import java.util.Collection;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import java.util.Set;
/*     */ import javax.servlet.http.Cookie;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import org.apache.commons.httpclient.HostConfiguration;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.HttpException;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.lang.StringUtils;
/*     */ 
/*     */ public class SimN38NetUtils
/*     */ {
/*     */   private static SimN38NetUtils instancia;
/*     */ 
/*     */   public static SimN38NetUtils getInstance()
/*     */   {
/*  30 */     if (instancia == null) {
/*  31 */       instancia = new SimN38NetUtils();
/*     */     }
/*     */ 
/*  34 */     return instancia;
/*     */   }
/*     */ 
/*     */   public InputStream realizarPeticionURL(String ruta, Map headerProperties)
/*     */   {
/*  39 */     return realizarPeticionURL(ruta, headerProperties, null, null, null);
/*     */   }
/*     */ 
/*     */   public InputStream realizarPeticionURL(String ruta, Map headerProperties, String proxyHost, String proxyPort)
/*     */   {
/*  44 */     return realizarPeticionURL(ruta, headerProperties, null, proxyHost, proxyPort);
/*     */   }
/*     */ 
/*     */   public InputStream realizarPeticionURL(String ruta, Map headerProperties, Map requestProperties)
/*     */   {
/*  49 */     return realizarPeticionURL(ruta, headerProperties, requestProperties, null, null);
/*     */   }
/*     */ 
/*     */   public InputStream realizarPeticionURL(String ruta, Map headerProperties, Map requestProperties, String proxyHost, String proxyPort)
/*     */   {
/*     */     try
/*     */     {
/*     */       boolean usarProxy;
/*     */       boolean usarProxy;
/*  61 */       if ((StringUtils.isNotBlank(proxyHost)) && 
/*  62 */         (StringUtils.isNotBlank(proxyPort)))
/*  63 */         usarProxy = true;
/*     */       else {
/*  65 */         usarProxy = false;
/*     */       }
/*     */ 
/*  71 */       String rutaCompleta = addParamsToUrl(ruta, requestProperties);
/*     */ 
/*  73 */       HttpClient httpClient = new HttpClient();
/*     */ 
/*  77 */       HostConfiguration hostConfiguration = new HostConfiguration();
/*     */ 
/*  82 */       if (usarProxy) {
/*  83 */         hostConfiguration.setProxy(proxyHost, 
/*  84 */           Integer.parseInt(proxyPort));
/*     */       }
/*     */ 
/*  87 */       httpClient.setHostConfiguration(hostConfiguration);
/*     */ 
/*  89 */       httpClient.executeMethod(new GetMethod(rutaCompleta));
/*     */ 
/*  91 */       URL url = new URL(rutaCompleta);
/*     */ 
/*  93 */       URLConnection urlConn = url.openConnection();
/*     */ 
/*  98 */       addHeaders(urlConn, headerProperties);
/*     */ 
/* 100 */       if (usarProxy)
/*     */       {
/* 105 */         synchronized (System.getProperties())
/*     */         {
/* 107 */           String systemProxySet = System.getProperties()
/* 108 */             .getProperty("http.proxySet");
/* 109 */           String systemProxyHost = System.getProperties()
/* 110 */             .getProperty("http.proxyHost");
/* 111 */           String systemProxyPort = System.getProperties()
/* 112 */             .getProperty("http.proxyPort");
/*     */ 
/* 114 */           System.getProperties().put("http.proxySet", "true");
/* 115 */           System.getProperties().put("http.proxyHost", proxyHost);
/* 116 */           System.getProperties().put("http.proxyPort", proxyPort);
/*     */ 
/* 118 */           urlConn.connect();
/*     */ 
/* 120 */           if (StringUtils.isNotBlank(systemProxySet))
/* 121 */             System.getProperties().put("http.proxySet", 
/* 122 */               systemProxySet);
/*     */           else {
/* 124 */             System.getProperties()
/* 125 */               .put("http.proxySet", "false");
/*     */           }
/*     */ 
/* 128 */           if (StringUtils.isNotBlank(systemProxyHost))
/* 129 */             System.getProperties().put("http.proxyHost", 
/* 130 */               systemProxyHost);
/*     */           else {
/* 132 */             System.getProperties().put("http.proxyHost", "");
/*     */           }
/*     */ 
/* 135 */           if (StringUtils.isNotBlank(systemProxyPort))
/* 136 */             System.getProperties().put("http.proxyPort", 
/* 137 */               systemProxyPort);
/*     */           else {
/* 139 */             System.getProperties().put("http.proxyPort", "");
/*     */           }
/*     */ 
/*     */         }
/*     */ 
/*     */       }
/*     */ 
/* 147 */       urlConn.connect();
/*     */ 
/* 151 */       return urlConn.getInputStream();
/*     */     } catch (HttpException e) {
/* 153 */       throw new SimN38NetException("No se a podido realizar una conexion correctamente"); } catch (IOException e) {
/*     */     }
/* 155 */     throw new SimN38NetException("No se a podido realizar una conexion correctamente");
/*     */   }
/*     */ 
/*     */   public String getValorCookie(HttpServletRequest request, String nombreCookie)
/*     */   {
/* 161 */     Cookie[] cookies = request.getCookies();
/*     */ 
/* 163 */     if (cookies != null)
/*     */     {
/* 165 */       for (int i = 0; i < cookies.length; i++)
/*     */       {
/* 167 */         String nombre = cookies[i].getName();
/*     */ 
/* 169 */         if (nombre.equals(nombreCookie)) {
/* 170 */           return cookies[i].getValue();
/*     */         }
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 176 */     return null;
/*     */   }
/*     */ 
/*     */   private String addParamsToUrl(String ruta, Map requestProperties)
/*     */   {
/* 182 */     StringBuffer rutaBuffer = new StringBuffer(ruta);
/*     */ 
/* 190 */     if ((requestProperties != null) && (!requestProperties.isEmpty())) {
/* 191 */       rutaBuffer.append("?");
/*     */ 
/* 193 */       Set keys = requestProperties.keySet();
/* 194 */       Collection values = requestProperties.values();
/*     */ 
/* 196 */       boolean primera = true;
/* 197 */       Iterator itKeys = keys.iterator(); for (Iterator itValues = values.iterator(); itKeys.hasNext(); ) {
/* 198 */         String strKey = (String)itKeys.next();
/* 199 */         String strValue = (String)itValues.next();
/*     */ 
/* 201 */         if (!primera)
/* 202 */           rutaBuffer.append("&");
/*     */         else {
/* 204 */           primera = false;
/*     */         }
/*     */ 
/* 207 */         rutaBuffer.append(strKey).append("=").append(strValue);
/*     */       }
/*     */     }
/*     */ 
/* 211 */     return rutaBuffer.toString();
/*     */   }
/*     */ 
/*     */   private void addHeaders(URLConnection urlConn, Map headerProperties)
/*     */   {
/* 216 */     Set keys = headerProperties.keySet();
/* 217 */     Collection values = headerProperties.values();
/*     */ 
/* 222 */     Iterator itKeys = keys.iterator(); for (Iterator itValues = values.iterator(); itKeys.hasNext(); ) {
/* 223 */       String strKey = (String)itKeys.next();
/* 224 */       String strValue = (String)itValues.next();
/*     */ 
/* 226 */       urlConn.setRequestProperty(strKey, strValue);
/*     */     }
/*     */   }
/*     */ }

/* Location:           /home/EUROHELP-BILBAO/acarbajo/usr/j2se/xlnets/aplic/n38/lib/weblogic81/n38_eneko.jar
 * Qualified Name:     n38sim.net.SimN38NetUtils
 * JD-Core Version:    0.6.0
 */