Java tutorial
/* * 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.artifact.web.remote; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import io.curly.artifact.model.Artifact; import io.curly.commons.web.hateoas.RemoteLinkCatcher; import io.curly.commons.web.hateoas.ZuulAwareMappingResolver; import lombok.extern.slf4j.Slf4j; 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 org.springframework.util.StringUtils; import javax.servlet.http.HttpServletRequest; import java.net.URI; import java.net.URISyntaxException; /** * @author Joao Pedro Evangelista */ @Slf4j @Component class PaperclipLinkCatcher implements RemoteLinkCatcher<Artifact> { private final LoadBalancerClient loadBalancerClient; private final ZuulAwareMappingResolver zuulAwareMappingResolver; @Autowired PaperclipLinkCatcher(LoadBalancerClient loadBalancerClient, HttpServletRequest request) { this.loadBalancerClient = loadBalancerClient; this.zuulAwareMappingResolver = new ZuulAwareMappingResolver(request, "/api", "/papers"); } @Override @HystrixCommand(fallbackMethod = "noOpLink") public Link getLink(Artifact entity) { String paperclip = zuulAwareMappingResolver.resolve(loadBalancerClient.choose("paperclip").getUri(), entity.getId()); log.debug("Found remote link {} from client Paperclip", paperclip); return new Link(paperclip, "paper"); } public Link noOpLink(Object entity) { log.debug("Remote link failed! Returning a null!"); return null; } @Override public boolean isAvaliable() { return true; } private String reconstructURI(String host, String href) { URI original; try { original = new URI(href); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot create URI from: " + href); } int port = 80; if ("https".equals(original.getScheme())) { port = 443; } if (host.contains(":")) { String[] pair = host.split(":"); host = pair[0]; port = Integer.valueOf(pair[1]); } if (host.equals(original.getHost()) && port == original.getPort()) { return href; } String scheme = original.getScheme(); if (scheme == null) { scheme = port == 443 ? "https" : "http"; } StringBuilder sb = new StringBuilder(); sb.append(scheme).append("://"); if (StringUtils.hasText(original.getRawUserInfo())) { sb.append(original.getRawUserInfo()).append("@"); } sb.append(host); if (port >= 0) { sb.append(":").append(port); } sb.append(original.getPath()); if (StringUtils.hasText(original.getRawQuery())) { sb.append("?").append(original.getRawQuery()); } if (StringUtils.hasText(original.getRawFragment())) { sb.append("#").append(original.getRawFragment()); } return sb.toString(); } }