io.curly.paperclip.paper.web.hateoas.ArtifactoryLinkCatcher.java Source code

Java tutorial

Introduction

Here is the source code for io.curly.paperclip.paper.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.paperclip.paper.web.hateoas;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.curly.commons.web.hateoas.RemoteLinkCatcher;
import io.curly.commons.web.hateoas.ZuulAwareMappingResolver;
import io.curly.paperclip.paper.model.Paper;
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
class ArtifactoryLinkCatcher implements RemoteLinkCatcher<Paper> {

    private final LoadBalancerClient loadBalancerClient;

    private final ZuulAwareMappingResolver zuulAwareMappingResolver;

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

    @Override
    @HystrixCommand(fallbackMethod = "noOpLink")
    public Link getLink(Paper entity) {
        String artifactory = zuulAwareMappingResolver.resolve(loadBalancerClient.choose("artifactory").getUri(),
                entity.getItem());
        return new Link(artifactory, "artifact");
    }

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