summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/jersey/xml/RestApiBuilder.java
blob: 4aa5882119f61d65451e907ab0616ff8754653cc (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.jersey.xml;

import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.producer.AbstractConfigProducer;
import com.yahoo.text.XML;
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder;
import com.yahoo.vespa.model.container.jersey.RestApi;
import com.yahoo.vespa.model.container.jersey.RestApiContext;
import org.w3c.dom.Element;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author gjoranv
 * @since 5.6
 */
public class RestApiBuilder extends VespaDomBuilder.DomConfigProducerBuilder<RestApi>  {

    @Override
    protected RestApi doBuild(DeployState deployState, AbstractConfigProducer ancestor, Element spec) {
        String bindingPath = spec.getAttribute("path");
        RestApi restApi = new RestApi(bindingPath);

        restApi.setRestApiContext(createRestApiContext(ancestor, spec, bindingPath));
        return restApi;
    }

    private RestApiContext createRestApiContext(AbstractConfigProducer ancestor, Element spec, String bindingPath) {
        RestApiContext restApiContext = new RestApiContext(ancestor, bindingPath);

        restApiContext.addBundles(getBundles(spec));

        return restApiContext;
    }

    private List<RestApiContext.BundleInfo> getBundles(Element spec) {
        List<RestApiContext.BundleInfo> bundles = new ArrayList<>();
        for (Element bundleElement : XML.getChildren(spec, "components")) {
            bundles.add(getBundle(bundleElement));
        }
        return bundles;
    }

    private RestApiContext.BundleInfo getBundle(Element bundleElement) {
        RestApiContext.BundleInfo bundle = new RestApiContext.BundleInfo(bundleElement.getAttribute("bundle"));

        for (Element packageElement : XML.getChildren(bundleElement, "package"))
            bundle.addPackageToScan(XML.getValue(packageElement));

        return bundle;
    }

    // TODO: use for naming injected components instead
    private Map<String, String> getInjections(Element spec) {
        Map<String, String> injectForClass = new LinkedHashMap<>();
        for (Element injectElement : XML.getChildren(spec, "inject")) {
            injectForClass.put(injectElement.getAttribute("for-class"),
                               injectElement.getAttribute("component"));
        }
        return injectForClass;
    }

}