001/* 002 * Copyright 2012-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.devtools.remote.server; 018 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.Collection; 022import java.util.List; 023 024import org.springframework.core.annotation.AnnotationAwareOrderComparator; 025import org.springframework.http.HttpStatus; 026import org.springframework.http.server.ServerHttpRequest; 027import org.springframework.http.server.ServerHttpResponse; 028import org.springframework.util.Assert; 029 030/** 031 * Dispatcher used to route incoming remote server requests to a {@link Handler}. Similar 032 * to {@code DispatchServlet} in Spring MVC but separate to ensure that remote support can 033 * be used regardless of any web framework. 034 * 035 * @author Phillip Webb 036 * @since 1.3.0 037 * @see HandlerMapper 038 */ 039public class Dispatcher { 040 041 private final AccessManager accessManager; 042 043 private final List<HandlerMapper> mappers; 044 045 public Dispatcher(AccessManager accessManager, Collection<HandlerMapper> mappers) { 046 Assert.notNull(accessManager, "AccessManager must not be null"); 047 Assert.notNull(mappers, "Mappers must not be null"); 048 this.accessManager = accessManager; 049 this.mappers = new ArrayList<>(mappers); 050 AnnotationAwareOrderComparator.sort(this.mappers); 051 } 052 053 /** 054 * Dispatch the specified request to an appropriate {@link Handler}. 055 * @param request the request 056 * @param response the response 057 * @return {@code true} if the request was dispatched 058 * @throws IOException in case of I/O errors 059 */ 060 public boolean handle(ServerHttpRequest request, ServerHttpResponse response) 061 throws IOException { 062 for (HandlerMapper mapper : this.mappers) { 063 Handler handler = mapper.getHandler(request); 064 if (handler != null) { 065 handle(handler, request, response); 066 return true; 067 } 068 } 069 return false; 070 } 071 072 private void handle(Handler handler, ServerHttpRequest request, 073 ServerHttpResponse response) throws IOException { 074 if (!this.accessManager.isAllowed(request)) { 075 response.setStatusCode(HttpStatus.FORBIDDEN); 076 return; 077 } 078 handler.handle(request, response); 079 } 080 081}