io.curly.advisor.web.hateoas.ArtifactoryLinkCatcher.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.advisor.web.hateoas.ArtifactoryLinkCatcher.java

Source

/*
 *        Copyright 2015 the original author or authors.
 *
 *    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 io.curly.advisor.web.hateoas;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.curly.advisor.model.Review;
import io.curly.commons.web.hateoas.RemoteLinkCatcher;
import io.curly.commons.web.hateoas.ZuulAwareMappingResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.hateoas.Link;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Joao Pedro Evangelista
 */
@Component
public class ArtifactoryLinkCatcher implements RemoteLinkCatcher<Review> {

    private final LoadBalancerClient loadBalancerClient;

    private final ZuulAwareMappingResolver resolver;

    @Autowired
    public ArtifactoryLinkCatcher(HttpServletRequest request, LoadBalancerClient loadBalancerClient) {
        this.loadBalancerClient = loadBalancerClient;
        resolver = new ZuulAwareMappingResolver(request, "/api", "/artifacts");
    }

    @Override
    @HystrixCommand(fallbackMethod = "noOpLink")
    public Link getLink(Review review) {
        final String artifactory = resolver.resolve(loadBalancerClient.choose("artifactory").getUri(),
                review.getArtifact());
        return new Link(artifactory, "artifact");
    }

    public Link noOpLink(Object entitiy) {
        return null;
    }
}