• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.cts.tradefed.result;
18 
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 
24 import javax.annotation.Nullable;
25 
26 /**
27  * Class that sends a HTTP POST multipart/form-data request containing
28  * the test result XML.
29  */
30 class ResultReporter {
31 
32     private static final int RESULT_XML_BYTES = 500 * 1024;
33 
34     private final String mServerUrl;
35     private final String mSuiteName;
36 
ResultReporter(String serverUrl, String suiteName)37     ResultReporter(String serverUrl, String suiteName) {
38         mServerUrl = serverUrl;
39         mSuiteName = suiteName;
40     }
41 
reportResult(File reportFile, @Nullable String referenceUrl)42     public void reportResult(File reportFile, @Nullable String referenceUrl) throws IOException {
43         if (isEmpty(mServerUrl)) {
44             return;
45         }
46 
47         InputStream input = new FileInputStream(reportFile);
48         try {
49             byte[] data = IssueReporter.getBytes(input, RESULT_XML_BYTES);
50             MultipartForm multipartForm = new MultipartForm(mServerUrl)
51                     .addFormValue("suite", mSuiteName)
52                     .addFormFile("resultXml", "testResult.xml.gz", data);
53             if (!isEmpty(referenceUrl)) {
54                 multipartForm.addFormValue("referenceUrl", referenceUrl);
55             }
56             multipartForm.submit();
57         } finally {
58             input.close();
59         }
60     }
61 
isEmpty(String value)62     private static boolean isEmpty(String value) {
63         return value == null || value.trim().isEmpty();
64     }
65 }
66