buildframework/helium/sf/java/diamonds/src/com/nokia/helium/diamonds/DiamondsClient.java
changeset 628 7c4a911dc066
parent 588 c7c26511138f
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
     1 /*
     1 /*
     2 * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3  * All rights reserved.
     4 * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     5  * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     8  *
     9 * Initial Contributors:
     9  * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    10  * Nokia Corporation - initial contribution.
    11 *
    11  *
    12 * Contributors:
    12  * Contributors:
    13 *
    13  *
    14 * Description:  
    14  * Description:  
    15 *
    15  *
    16 */
    16  */
    17 
       
    18 
    17 
    19 package com.nokia.helium.diamonds;
    18 package com.nokia.helium.diamonds;
    20 
    19 
       
    20 import java.io.File;
       
    21 import java.io.IOException;
       
    22 import java.io.InputStream;
    21 import org.apache.commons.httpclient.HttpClient;
    23 import org.apache.commons.httpclient.HttpClient;
    22 import org.apache.commons.httpclient.HttpException;
    24 import org.apache.commons.httpclient.HttpException;
    23 import org.apache.commons.httpclient.methods.FileRequestEntity;
    25 import org.apache.commons.httpclient.methods.FileRequestEntity;
    24 import org.apache.commons.httpclient.methods.PostMethod;
    26 import org.apache.commons.httpclient.methods.PostMethod;
    25 import org.apache.commons.httpclient.methods.RequestEntity;
    27 import org.apache.commons.httpclient.methods.RequestEntity;
       
    28 import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
    26 import org.apache.commons.lang.StringUtils;
    29 import org.apache.commons.lang.StringUtils;
    27 import org.apache.log4j.Logger;
    30 import org.apache.log4j.Logger;
       
    31 
    28 import com.nokia.helium.core.EmailDataSender;
    32 import com.nokia.helium.core.EmailDataSender;
    29 
    33 import com.nokia.helium.core.EmailSendException;
    30 import java.io.IOException;
       
    31 import java.io.File;
       
    32 
    34 
    33 /**
    35 /**
    34  * Diamonds client used to connect to get build id and also to send the build
    36  * Diamonds client used to connect to get build id and also to send the build results
    35  * results
       
    36  * 
    37  * 
    37  */
    38  */
    38 public class DiamondsClient {
    39 public class DiamondsClient {
    39 
    40 
    40     private static final int INT_SERV_ERROR = 500;
    41     private static final int INT_SERV_ERROR = 500;
    41 
    42 
    42     private static final int SERV_NOT_FOUND = 404;
    43     private static final int SERV_NOT_FOUND = 404;
    43 
    44 
    44     private static final int SERV_OK = 200;
    45     private static final int SERV_OK = 200;
    45     
    46 
    46     private boolean isRecordOnly;
    47     private boolean isRecordOnly;
    47 
    48 
    48     private Logger log = Logger.getLogger(DiamondsClient.class);
    49     private Logger log = Logger.getLogger(DiamondsClient.class);
    49 
    50 
    50     private String host;
    51     private String host;
    54     private String path;
    55     private String path;
    55 
    56 
    56     private String emailID;
    57     private String emailID;
    57 
    58 
    58     private HttpClient httpClient;
    59     private HttpClient httpClient;
    59 
       
    60 
    60 
    61     public DiamondsClient(String hst, String prt, String pth, String mailID) {
    61     public DiamondsClient(String hst, String prt, String pth, String mailID) {
    62         host = hst;
    62         host = hst;
    63         port = prt;
    63         port = prt;
    64         path = pth;
    64         path = pth;
    68 
    68 
    69     private int executeMethod(PostMethod postMethod) throws DiamondsException {
    69     private int executeMethod(PostMethod postMethod) throws DiamondsException {
    70         int result = 0;
    70         int result = 0;
    71         try {
    71         try {
    72             result = httpClient.executeMethod(postMethod);
    72             result = httpClient.executeMethod(postMethod);
    73         } catch (IOException e) {
    73         }
       
    74         catch (IOException e) {
    74             isRecordOnly = true;
    75             isRecordOnly = true;
    75             throw new DiamondsException("IOException while sending http request." + e.getMessage());
    76             throw new DiamondsException("IOException while sending http request." + e.getMessage());
    76             // e.printStackTrace();
    77             // e.printStackTrace();
    77         }
    78         }
    78         return result;
    79         return result;
   107         PostMethod post = new PostMethod(urlPath);
   108         PostMethod post = new PostMethod(urlPath);
   108 
   109 
   109         // Request content will be retrieved directly
   110         // Request content will be retrieved directly
   110         // from the input stream
   111         // from the input stream
   111 
   112 
   112         RequestEntity entity = new FileRequestEntity(input,
   113         RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
   113                 "text/xml; charset=ISO-8859-1");
       
   114         post.setRequestEntity(entity);
   114         post.setRequestEntity(entity);
   115         return post;
   115         return post;
   116     }
   116     }
   117 
   117 
   118     private int processPostMethodResult(int result) {
   118     private int processPostMethodResult(int result) {
   119         // Log status code
   119         // Log status code
   120         switch (result) {
   120         switch (result) {
   121         case INT_SERV_ERROR:
   121             case INT_SERV_ERROR:
   122             // log.error("Internal server error");
   122                 // log.error("Internal server error");
   123             break;
   123                 break;
   124         case SERV_NOT_FOUND:
   124             case SERV_NOT_FOUND:
   125             // log.error("Server not found");
   125                 // log.error("Server not found");
   126             break;
   126                 break;
   127         case SERV_OK:
   127             case SERV_OK:
   128             // log.info("Connection to diamonds server - OK");
   128                 // log.info("Connection to diamonds server - OK");
   129             break;
   129                 break;
   130         default:
   130             default:
   131             // log.debug("Response code: " + result);
   131                 // log.debug("Response code: " + result);
   132         }
   132         }
   133         return result;
   133         return result;
   134     }
   134     }
   135 
   135     
       
   136     public String getBuildId(InputStream stream) throws DiamondsException {
       
   137         String diamondsBuildID = null;
       
   138         PostMethod postMethod = null;
       
   139         try {
       
   140             if (!isRecordOnly) {
       
   141                 String strURL = getURL();
       
   142                 log.debug("strURL:" + strURL);
       
   143                 postMethod = getPostMethod(stream, strURL);
       
   144                 log.debug("postmethod:" + postMethod);
       
   145                 int postMethodResult = httpClient.executeMethod(postMethod);
       
   146                 log.debug("postmethod-result:" + postMethodResult);
       
   147 
       
   148                 int result = processPostMethodResult(postMethodResult);
       
   149 
       
   150                 if (result == SERV_OK) {
       
   151                     // Display and save response code which functions as a id for
       
   152                     // the build.
       
   153                     diamondsBuildID = postMethod.getResponseBodyAsString();
       
   154                     log.debug("diamondsBuildID: " + diamondsBuildID);
       
   155                 }
       
   156                 else {
       
   157                     isRecordOnly = true;
       
   158                     log.error("Diamonds data not sent, because of connection failure.");
       
   159                     // throw new DiamondsException("Connection Failed");
       
   160                 }
       
   161             }
       
   162         }
       
   163         catch (HttpException ex) {
       
   164             isRecordOnly = true;
       
   165             log.debug("Diamonds data not sent:s", ex);
       
   166             log.error("Diamonds data not sent: " + ex.getMessage());
       
   167         }
       
   168         catch (IOException ex) {
       
   169             isRecordOnly = true;
       
   170             log.debug("Diamonds data not sent:", ex);
       
   171             log.error("Diamonds data not sent: " + ex.getMessage());
       
   172         }
       
   173         finally {
       
   174             // Release current connection to the connection pool once you are
       
   175             // done
       
   176             if (postMethod != null) {
       
   177                 postMethod.releaseConnection();
       
   178             }
       
   179         }
       
   180         return diamondsBuildID;
       
   181     }
   136 
   182 
   137     /**
   183     /**
   138      * 
   184      * 
   139      * @param fileName
   185      * @param fileName Filename to export to Diamonds
   140      *            Filename to export to Diamonds
       
   141      * @return diamonds build id
   186      * @return diamonds build id
   142      */
   187      */
   143     public String getBuildId(String fileName) throws DiamondsException {
   188     public String getBuildId(String fileName) throws DiamondsException {
   144         String diamondsBuildID = null;
   189         String diamondsBuildID = null;
   145         PostMethod postMethod = null;
   190         PostMethod postMethod = null;
   154                 log.debug("strURL:" + strURL);
   199                 log.debug("strURL:" + strURL);
   155                 postMethod = getPostMethod(fileName, strURL);
   200                 postMethod = getPostMethod(fileName, strURL);
   156                 log.debug("postmethod:" + postMethod);
   201                 log.debug("postmethod:" + postMethod);
   157                 int postMethodResult = httpClient.executeMethod(postMethod);
   202                 int postMethodResult = httpClient.executeMethod(postMethod);
   158                 log.debug("postmethod-result:" + postMethodResult);
   203                 log.debug("postmethod-result:" + postMethodResult);
   159     
   204 
   160                 int result = processPostMethodResult(postMethodResult);
   205                 int result = processPostMethodResult(postMethodResult);
   161 
   206 
   162                 if (result == SERV_OK) {
   207                 if (result == SERV_OK) {
   163                     // Display and save response code which functions as a id for
   208                     // Display and save response code which functions as a id for
   164                     // the build.
   209                     // the build.
   165                     diamondsBuildID = postMethod.getResponseBodyAsString();
   210                     diamondsBuildID = postMethod.getResponseBodyAsString();
   166                     log.debug("diamondsBuildID: " + diamondsBuildID);
   211                     log.debug("diamondsBuildID: " + diamondsBuildID);
   167                 } else {
   212                 }
       
   213                 else {
   168                     isRecordOnly = true;
   214                     isRecordOnly = true;
   169                     log.error("Diamonds data not sent, because of connection failure.");
   215                     log.error("Diamonds data not sent, because of connection failure.");
   170                     //throw new DiamondsException("Connection Failed");
   216                     // throw new DiamondsException("Connection Failed");
   171                 }
   217                 }
   172             }
   218             }
   173         } catch (HttpException ex) {
   219         }
   174             isRecordOnly = true;
   220         catch (HttpException ex) {
       
   221             isRecordOnly = true;
       
   222             log.debug("Diamonds data not sent:s", ex);
   175             log.error("Diamonds data not sent: " + ex.getMessage());
   223             log.error("Diamonds data not sent: " + ex.getMessage());
   176             log.debug("Diamonds data not sent:s", ex);
   224         }
   177         } catch (IOException ex) {
   225         catch (IOException ex) {
   178             isRecordOnly = true;
   226             isRecordOnly = true;
       
   227             log.debug("Diamonds data not sent:", ex);
   179             log.error("Diamonds data not sent: " + ex.getMessage());
   228             log.error("Diamonds data not sent: " + ex.getMessage());
   180             log.debug("Diamonds data not sent:", ex);
   229         }
   181         } finally {
   230         finally {
   182             // Release current connection to the connection pool once you are
   231             // Release current connection to the connection pool once you are
   183             // done
   232             // done
   184             if (postMethod != null) {
   233             if (postMethod != null) {
   185                 postMethod.releaseConnection();
   234                 postMethod.releaseConnection();
   186             }
   235             }
   187         }
   236         }
   188         return diamondsBuildID;
   237         return diamondsBuildID;
   189     }
   238     }
   190 
   239 
       
   240     private PostMethod getPostMethod(InputStream stream, String urlPath) {
       
   241 
       
   242         // Get the Diamonds XML-file which is to be exported
       
   243         //File input = new File(fileName);
       
   244 
       
   245         // Prepare HTTP post
       
   246         PostMethod post = new PostMethod(urlPath);
       
   247 
       
   248         RequestEntity entity = new InputStreamRequestEntity(stream, "text/xml");
       
   249         post.setRequestEntity(entity);
       
   250         return post;
       
   251     }
       
   252 
       
   253     public int sendData(InputStream stream, String urlPath) {
       
   254         PostMethod postMethod = null;
       
   255         int result = -1;
       
   256         if (urlPath != null && !isRecordOnly) {
       
   257             try {
       
   258                 String strURL = getURL(urlPath);
       
   259                 postMethod = getPostMethod(stream, strURL);
       
   260                 result = processPostMethodResult(httpClient.executeMethod(postMethod));
       
   261             }
       
   262             catch (IllegalArgumentException e) {
       
   263                 // Catching this exception is needed because it is raised by httpclient
       
   264                 // library if the server is under update.
       
   265                 log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
       
   266             }
       
   267             catch (DiamondsException e) {
       
   268                 log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
       
   269             }
       
   270             catch (IOException e) {
       
   271                 log.error("sendData:The final data via http not sent because errors:IOException ", e);
       
   272             }
       
   273         }
       
   274         return result;
       
   275     }
       
   276     
   191     public int sendData(String fileName, String urlPath) {
   277     public int sendData(String fileName, String urlPath) {
   192         PostMethod postMethod = null;
   278         PostMethod postMethod = null;
   193         int result = -1;
   279         int result = -1;
   194         if (urlPath != null && !isRecordOnly) {
   280         if (urlPath != null && !isRecordOnly) {
   195             try {
   281             try {
   196                 String strURL = getURL(urlPath);
   282                 String strURL = getURL(urlPath);
   197                 postMethod = getPostMethod(fileName, strURL);
   283                 postMethod = getPostMethod(fileName, strURL);
   198                 result = processPostMethodResult(httpClient
   284                 result = processPostMethodResult(httpClient.executeMethod(postMethod));
   199                         .executeMethod(postMethod));
   285             }
   200             } catch (IllegalArgumentException e) {
   286             catch (IllegalArgumentException e) {
   201                 // Catching this exception is needed because it is raised by httpclient
   287                 // Catching this exception is needed because it is raised by httpclient
   202                 // library if the server is under update.
   288                 // library if the server is under update.
   203                 log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
   289                 log.error("sendData:The final data via http not sent because errors:IllegalArgumentException ", e);
   204             } catch (DiamondsException e) {
   290             }
       
   291             catch (DiamondsException e) {
   205                 log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
   292                 log.error("sendData:The final data via http not sent because errors:DiamondsException ", e);
   206             } catch (IOException e) {
   293             }
       
   294             catch (IOException e) {
   207                 log.error("sendData:The final data via http not sent because errors:IOException ", e);
   295                 log.error("sendData:The final data via http not sent because errors:IOException ", e);
   208             }
   296             }
   209         }
   297         }
   210         return result;
   298         return result;
   211     }
   299     }
   212 
   300 
   213     public int sendDataByMail(String fileName, String smtpServer,
   301     public int sendDataByMail(String fileName, String smtpServer, String ldapServer) throws EmailSendException {
   214             String ldapServer) {
       
   215         log.debug("DiamondsClient:sendDataByEmail:emailID" + emailID);
   302         log.debug("DiamondsClient:sendDataByEmail:emailID" + emailID);
   216         EmailDataSender emailSender = new EmailDataSender(emailID, smtpServer,
   303         EmailDataSender emailSender = new EmailDataSender(emailID, smtpServer, ldapServer);
   217                 ldapServer);
       
   218         log.debug("DiamondsClient:sendDataByEmail: " + fileName);
   304         log.debug("DiamondsClient:sendDataByEmail: " + fileName);
   219         emailSender.sendData("diamonds", fileName, "application/xml",
   305         emailSender.sendData("diamonds", new File(fileName), "application/xml", "[DIAMONDS_DATA]", null);
   220                 "[DIAMONDS_DATA]", null);
       
   221         log.debug("DiamondsClient:sendDataByEmail:succeeds");
   306         log.debug("DiamondsClient:sendDataByEmail:succeeds");
   222         return 0;
   307         return 0;
   223     }
   308     }
   224 }
   309 }