Example usage for com.squareup.okhttp MultipartBuilder MIXED

List of usage examples for com.squareup.okhttp MultipartBuilder MIXED

Introduction

In this page you can find the example usage for com.squareup.okhttp MultipartBuilder MIXED.

Prototype

MediaType MIXED

To view the source code for com.squareup.okhttp MultipartBuilder MIXED.

Click Source Link

Document

The "mixed" subtype of "multipart" is intended for use when the body parts are independent and need to be bundled in a particular order.

Usage

From source file:com.xebialabs.xlt.ci.server.XLTestServerImpl.java

License:Open Source License

@Override
public void uploadTestRun(String testSpecificationId, FilePath workspace, String includes, String excludes,
        Map<String, Object> metadata, PrintStream logger) throws IOException, InterruptedException {
    if (testSpecificationId == null || testSpecificationId.isEmpty()) {
        throw new IllegalArgumentException(
                "No test specification id specified. Does the test specification still exist in XL TestView?");
    }/*from  w  ww  . java  2s  .  c o  m*/
    try {
        logInfo(logger,
                format("Collecting files from '%s' using include pattern: '%s' and exclude pattern '%s'",
                        workspace.getRemote(), includes, excludes));

        DirScanner scanner = new DirScanner.Glob(includes, excludes);

        ObjectMapper objectMapper = new ObjectMapper();

        RequestBody body = new MultipartBuilder().type(MultipartBuilder.MIXED)
                .addPart(RequestBody.create(MediaType.parse(APPLICATION_JSON_UTF_8),
                        objectMapper.writeValueAsString(metadata)))
                .addPart(new ZipRequestBody(workspace, scanner, logger)).build();

        Request request = new Request.Builder()
                .url(createSensibleURL(API_IMPORT + "/" + testSpecificationId, serverUrl))
                .header("User-Agent", getUserAgent()).header("Accept", APPLICATION_JSON_UTF_8)
                .header("Authorization", createCredentials()).header("Transfer-Encoding", "chunked").post(body)
                .build();

        Response response = client.newCall(request).execute();
        ObjectMapper mapper = createMapper();
        ImportError importError;
        switch (response.code()) {
        case 200:
            logInfo(logger, "Sent data successfully");
            return;
        case 304:
            logWarn(logger, "No new results were detected. Nothing was imported.");
            throw new IllegalStateException("No new results were detected. Nothing was imported.");
        case 400:
            importError = mapper.readValue(response.body().byteStream(), ImportError.class);
            throw new IllegalStateException(importError.getMessage());
        case 401:
            throw new AuthenticationException(String.format(
                    "User '%s' and the supplied password are unable to log in", credentials.getUsername()));
        case 402:
            throw new PaymentRequiredException("The XL TestView server does not have a valid license");
        case 404:
            throw new ConnectionException("Cannot find test specification '" + testSpecificationId
                    + ". Please check if the XL TestView server is "
                    + "running and the test specification exists.");
        case 422:
            logWarn(logger, "Unable to process results.");
            logWarn(logger,
                    "Are you sure your include/exclude pattern provides all needed files for the test tool?");
            importError = mapper.readValue(response.body().byteStream(), ImportError.class);
            throw new IllegalStateException(importError.getMessage());
        default:
            throw new IllegalStateException("Unknown error. Status code: " + response.code()
                    + ". Response message: " + response.toString());
        }
    } catch (URISyntaxException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        e.printStackTrace();
        LOG.warn("I/O error uploading test run data to {} {}\n{}", serverUrl.toString(), e.toString(), e);
        throw new IOException(
                "I/O error uploading test run data to " + serverUrl.toString() + " " + e.toString(), e);
    }
}