aboutsummaryrefslogtreecommitdiffstats
path: root/jaxrs_client_utils/src/main/java/com/yahoo/vespa/jaxrs/client/NoRetryJaxRsStrategy.java
blob: 27a8438ea1105b419e6093c6809cb96b166f9af0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.jaxrs.client;

import com.yahoo.vespa.applicationmodel.HostName;

import javax.ws.rs.ProcessingException;
import java.io.IOException;
import java.util.Objects;
import java.util.function.Function;

/**
 * A {@link JaxRsStrategy} that will try API calls once against a single server, giving up immediately on failure.
 *
 * @author bakksjo
 */
public class NoRetryJaxRsStrategy<T> implements JaxRsStrategy<T> {
    private final HostName hostName;
    private final int port;
    private final JaxRsClientFactory jaxRsClientFactory;
    private final Class<T> apiClass;
    private String pathPrefix;

    public NoRetryJaxRsStrategy(
            final HostName hostName,
            final int port,
            final JaxRsClientFactory jaxRsClientFactory,
            final Class<T> apiClass,
            final String pathPrefix) {
        Objects.requireNonNull(hostName, "hostName argument may not be null");
        Objects.requireNonNull(jaxRsClientFactory, "jaxRsClientFactory argument may not be null");
        Objects.requireNonNull(apiClass, "apiClass argument may not be null");
        Objects.requireNonNull(pathPrefix, "pathPrefix argument may not be null");
        this.hostName = hostName;
        this.port = port;
        this.jaxRsClientFactory = jaxRsClientFactory;
        this.apiClass = apiClass;
        this.pathPrefix = pathPrefix;
    }

    @Override
    public <R> R apply(final Function<T, R> function) throws IOException {
        final T jaxRsClient = jaxRsClientFactory.createClient(apiClass, hostName, port, pathPrefix);
        try {
            return function.apply(jaxRsClient);
        } catch (ProcessingException e) {
            throw new IOException("Communication with REST server failed", e);
        }
    }
}