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