All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zalando.stups.clients.kio.spring.RestTemplateKioOperations Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Copyright (C) 2015 Zalando SE (http://tech.zalando.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zalando.stups.clients.kio.spring;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.ParameterizedTypeReference;

import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import org.zalando.stups.clients.kio.Application;
import org.zalando.stups.clients.kio.ApplicationBase;
import org.zalando.stups.clients.kio.Approval;
import org.zalando.stups.clients.kio.ApprovalBase;
import org.zalando.stups.clients.kio.CreateOrUpdateApplicationRequest;
import org.zalando.stups.clients.kio.CreateOrUpdateVersionRequest;
import org.zalando.stups.clients.kio.KioOperations;
import org.zalando.stups.clients.kio.Version;
import org.zalando.stups.clients.kio.VersionBase;

/**
 * Implementation of {@link KioOperations} that uses Springs {@link RestTemplate}.
 *
 * @author  jbellmann
 */
public class RestTemplateKioOperations implements KioOperations {

    private final RestOperations restOperations;

    private final String baseUrl;

    private final ParameterizedTypeReference> applicationBaseParameterizedType =
        new ParameterizedTypeReference>() { };

    private final ParameterizedTypeReference> versionBaseParameterizedType =
        new ParameterizedTypeReference>() { };

    private final ParameterizedTypeReference> approvalParameterizedType =
        new ParameterizedTypeReference>() { };

    public RestTemplateKioOperations(final RestOperations restOperations, final String baseUrl) {
        this.restOperations = restOperations;
        this.baseUrl = baseUrl;
    }

    @Override
    public List listApplications() {
        ResponseEntity> response = getRestOperations().exchange(baseUrl + "/apps", HttpMethod.GET,
                null, applicationBaseParameterizedType);

        return response.getBody();
    }

    @Override
    public Application getApplicationById(final String applicationId) {
        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);

        return getRestOperations().getForObject(baseUrl + "/apps/{applicationId}", Application.class, uriVariables);
    }

    @Override
    public void createOrUpdateApplication(final CreateOrUpdateApplicationRequest request, final String applicationId) {
        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);

        getRestOperations().put(baseUrl + "/apps/{applicationId}", request);
    }

    @Override
    public List getApplicationApprovals(final String applicationId) {
        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);

        //
        return getRestOperations().getForObject(baseUrl + "/apps/{applicationId}", List.class, uriVariables);
    }

    @Override
    public List getApplicationVersions(final String applicationId) {

        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);

        ResponseEntity> response = getRestOperations().exchange(baseUrl
                    + "/apps/{applicationId}/versions", HttpMethod.GET, null, versionBaseParameterizedType,
                uriVariables);

        return response.getBody();

    }

    @Override
    public Version getApplicationVersion(final String applicationId, final String versionId) {

        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);
        uriVariables.put("versionId", versionId);

        return getRestOperations().getForObject(baseUrl + "/apps/{applicationId}/versions/{versionId}", Version.class,
                uriVariables);
    }

    @Override
    public void createOrUpdateVersion(final CreateOrUpdateVersionRequest request, final String applicationId,
            final String versionId) {

        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);
        uriVariables.put("versionId", versionId);

        getRestOperations().put(baseUrl + "/apps/{applicationId}/versions/{versionId}", request, uriVariables);
    }

    @Override
    public List getApplicationApprovals(final String applicationId, final String versionId) {

        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);
        uriVariables.put("versionId", versionId);

        ResponseEntity> response = getRestOperations().exchange(baseUrl
                    + "/apps/{applicationId}/versions/{versionId}", HttpMethod.GET, null, approvalParameterizedType,
                uriVariables);

        return response.getBody();
    }

    @Override
    public void approveApplicationVersion(final ApprovalBase request, final String applicationId,
            final String versionId) {

        Map uriVariables = new HashMap();
        uriVariables.put("applicationId", applicationId);
        uriVariables.put("versionId", versionId);

        getRestOperations().put(baseUrl + "/apps/{applicationId}/versions/{versionsId}", request, uriVariables);
    }

    protected RestOperations getRestOperations() {
        return this.restOperations;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy