top.lionxxw.zuulservice.filter.BookingCarRoutingFilter.java Source code

Java tutorial

Introduction

Here is the source code for top.lionxxw.zuulservice.filter.BookingCarRoutingFilter.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package top.lionxxw.zuulservice.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.logging.Logger;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.DEBUG_FILTER_ORDER;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE;

/**
 *
 * @author darren.shuxing.liu
 */
@Component
public class BookingCarRoutingFilter extends ZuulFilter {
    @Value("${unauthorized.url.redirect:http://www.lionxxw.top}")
    private String urlRedirect;
    /**
     *
     */
    protected static final Logger logger = Logger.getLogger(BookingCarRoutingFilter.class.getName());

    /**
     * 
     *pre?
     *routing
     *post ?
     *error??
     * @return String
     */
    @Override
    public String filterType() {
        return ROUTE_TYPE;
    }

    @Override
    public int filterOrder() {
        return DEBUG_FILTER_ORDER;
    }

    /*
    *??
    */
    @Override
    public boolean shouldFilter() {
        //
        return true;
    }

    /**
     * ??.
     *
     * @param request ??
     *
     * @return true?????false
     */
    private boolean isAuthorizedRequest(RequestContext request) {
        // 
        return false;
    }

    /**
     * This method allows to set the route host into the Zuul request context provided as parameter.
     * The url is extracted from the orginal request and the host is extracted from it.
     *
     * @param ctx Zuul
     *
     * @throws MalformedURLException
     */
    private void setRouteHost(RequestContext ctx) throws MalformedURLException {

        String urlS = ctx.getRequest().getRequestURL().toString();
        URL url = new URL(urlS);
        String protocol = url.getProtocol();
        String rootHost = url.getHost();
        int port = url.getPort();
        String portS = (port > 0 ? ":" + port : "");
        ctx.setRouteHost(new URL(protocol + "://" + rootHost + portS));

    }

    /**
     * The filter execution
     *
     * @return
     */
    @Override
    public Object run() {

        // ?
        logger.info("query interception");

        // ?Zuul
        RequestContext ctx = RequestContext.getCurrentContext();

        try {

            // if the requested url is authorized, the route host is set to the requested one
            if (isAuthorizedRequest(ctx)) {
                setRouteHost(ctx);
            } else {
                // if the requested URL is not authorized, the route host is set to the urlRedirect value
                // the client will be redirected to the new host
                ctx.setRouteHost(new URL(urlRedirect));
            }
        } catch (MalformedURLException e) {
            logger.info("error happened when routing request");
        }

        return null;
    }

    private String decode(String token) {
        byte[] decodedBytes = Base64.getDecoder().decode(token.getBytes());
        //byte[] encodedBytes = Base64.getEncoder().encode(origin.getBytes());
        return new String(decodedBytes);
    }
}