| /** |
| ****************************************************************************** |
| * Copyright © 2018 PTA GmbH. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| ****************************************************************************** |
| */ |
| |
| package org.eclipse.openK.core.controller; |
| |
| import com.google.common.collect.Lists; |
| import org.apache.log4j.Logger; |
| import org.eclipse.jetty.http.HttpStatus; |
| import org.eclipse.openK.api.ServiceDistributionCluster; |
| import org.eclipse.openK.api.ServiceDistributionCluster.ServiceDistribution; |
| import org.eclipse.openK.api.ServiceRequestEnvelope; |
| import org.eclipse.openK.core.communication.RestServiceWrapper; |
| import org.eclipse.openK.core.exceptions.HttpStatusException; |
| |
| import javax.ws.rs.core.Response; |
| import java.util.List; |
| |
| import static java.util.stream.Collectors.toList; |
| |
| public class DispatchController { |
| private static Logger logger = Logger.getLogger(DispatchController.class); |
| |
| public Response dispatch(String clustername, ServiceRequestEnvelope envelope) throws HttpStatusException { |
| RestServiceWrapper rsWrap = createRestServiceWrapper(envelope.isHttps()); |
| List<RestServiceWrapper.HttpHeader> transformedList = Lists.newArrayList(envelope.getHeaders()).stream() |
| .map(header -> |
| RestServiceWrapper.createHeader(header.getAttribute(), header.getValue())).collect(toList()); |
| |
| String url = locateBaseUrl(ServicesConfigCache.getInstance().getCache(), |
| clustername, envelope.getServiceName()) + "/" |
| + envelope.getUriFragment(); |
| |
| return rsWrap.performHttpRequest(resolveMethod(envelope.getMethod()), |
| url, transformedList, envelope.getPayloadDecode()); |
| } |
| |
| protected RestServiceWrapper createRestServiceWrapper(boolean useHttps) { |
| return new RestServiceWrapper(useHttps); |
| } |
| |
| private String locateBaseUrl(ServiceDistributionCluster[] cluster, String clustername, String servicename) throws HttpStatusException { |
| ServiceDistribution dist = new ServiceResolver(cluster) |
| .resolve(clustername, servicename); |
| if (dist == null) { |
| logger.error("Service [" + clustername + "]/[" + servicename + "] is not resolvable!"); |
| throw new HttpStatusException(HttpStatus.NOT_FOUND_404); |
| } |
| return dist.getProtocol() + "://" + dist.getHost() + ":" + dist.getPortApp() + dist.getUrlPath(); |
| } |
| |
| private RestServiceWrapper.HttpMethod resolveMethod(String method) throws HttpStatusException { |
| switch (method.toUpperCase()) { |
| case "GET": |
| return RestServiceWrapper.HttpMethod.GET; |
| case "POST": |
| return RestServiceWrapper.HttpMethod.POST; |
| case "PUT": |
| return RestServiceWrapper.HttpMethod.PUT; |
| case "DELETE": |
| return RestServiceWrapper.HttpMethod.DELETE; |
| default: |
| logger.error("Invalid Method: " + method); |
| throw new HttpStatusException(HttpStatus.METHOD_NOT_ALLOWED_405); |
| } |
| |
| } |
| } |