com.redhat.developers.msa.namaste.NamasteResource.java Source code

Java tutorial

Introduction

Here is the source code for com.redhat.developers.msa.namaste.NamasteResource.java

Source

/**
 * JBoss, Home of Professional Open Source
 * Copyright 2016, Red Hat, Inc. and/or its affiliates, and individual
 * contributors by the @authors tag. See the copyright.txt in the
 * distribution for a full listing of individual contributors.
 * <p/>
 * 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 com.redhat.developers.msa.namaste;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.github.kristofa.brave.Brave;
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler;
import com.github.kristofa.brave.ServerSpan;
import com.github.kristofa.brave.http.DefaultSpanNameProvider;
import com.github.kristofa.brave.http.HttpSpanCollector;
import com.github.kristofa.brave.httpclient.BraveHttpRequestInterceptor;
import com.github.kristofa.brave.httpclient.BraveHttpResponseInterceptor;

import feign.Logger;
import feign.Logger.Level;
import feign.httpclient.ApacheHttpClient;
import feign.hystrix.HystrixFeign;
import feign.jackson.JacksonDecoder;
import io.swagger.annotations.ApiOperation;

@Path("/")
public class NamasteResource {

    @GET
    @Path("/namaste")
    @Produces("text/plain")
    @ApiOperation("Returns the greeting in Indian")
    public String namaste() {
        String hostname = System.getenv().getOrDefault("HOSTNAME", "Unknown");
        return String.format("%s ke taraf se namaste", hostname);
    }

    @GET
    @Path("/namaste-chaining")
    @Produces("application/json")
    @ApiOperation("Returns the greeting plus the next service in the chain")
    public List<String> namasteChaining() {
        List<String> greetings = new ArrayList<>();
        greetings.add(namaste());
        greetings.addAll(getNextService().ola());
        return greetings;
    }

    @GET
    @Path("/health")
    @Produces("text/plain")
    @ApiOperation("Used to verify the health of the service")
    public String health() {
        return "I'm ok";
    }

    /**
     * This is were the "magic" happens: it creates a Feign, which is a proxy interface for remote calling a REST endpoint with
     * Hystrix fallback support.
     *
     * @return The feign pointing to the service URL and with Hystrix fallback.
     */
    private OlaService getNextService() {
        final String serviceName = "ola";
        final Brave brave = new Brave.Builder("namaste").spanCollector(HttpSpanCollector
                .create(System.getenv("ZIPKIN_SERVER_URL"), new EmptySpanCollectorMetricsHandler())).build();
        // This stores the Original/Parent ServerSpan from ZiPkin.
        final ServerSpan serverSpan = brave.serverSpanThreadBinder().getCurrentServerSpan();
        final CloseableHttpClient httpclient = HttpClients.custom()
                .addInterceptorFirst(new BraveHttpRequestInterceptor(brave.clientRequestInterceptor(),
                        new DefaultSpanNameProvider()))
                .addInterceptorFirst(new BraveHttpResponseInterceptor(brave.clientResponseInterceptor())).build();
        String url = String.format("http://%s:8080/", serviceName);
        return HystrixFeign.builder()
                // Use apache HttpClient which contains the ZipKin Interceptors
                .client(new ApacheHttpClient(httpclient))
                // Bind Zipkin Server Span to Feign Thread
                .requestInterceptor((t) -> brave.serverSpanThreadBinder().setCurrentSpan(serverSpan))
                .logger(new Logger.ErrorLogger()).logLevel(Level.BASIC).decoder(new JacksonDecoder())
                .target(OlaService.class, url, () -> Collections.singletonList("Ola response (fallback)"));
    }

}