All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.servicecomb.common.rest.locator.MicroservicePaths Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2017 Huawei Technologies Co., Ltd
 *
 * 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.servicecomb.common.rest.locator;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.servicecomb.common.rest.definition.RestOperationComparator;
import io.servicecomb.common.rest.definition.RestOperationMeta;
import io.servicecomb.foundation.common.exceptions.ServiceCombException;

public class MicroservicePaths {
  private static final Logger LOGGER = LoggerFactory.getLogger(MicroservicePaths.class);

  // 运行阶段,静态path,一次直接查找到目标,不必遍历查找
  // 以path为key
  protected Map staticPathOperations = new HashMap<>();

  // 运行阶段,以path优先级,从高到低排列的operation列表
  protected List dynamicPathOperationsList = new ArrayList<>();

  public void cloneTo(MicroservicePaths other) {
    other.staticPathOperations.putAll(staticPathOperations);
    other.dynamicPathOperationsList.addAll(dynamicPathOperationsList);
  }

  public void sortPath() {
    RestOperationComparator comparator = new RestOperationComparator();
    Collections.sort(this.dynamicPathOperationsList, comparator);
  }

  public void addResource(RestOperationMeta swaggerRestOperation) {
    if (swaggerRestOperation.isAbsoluteStaticPath()) {
      // 静态path
      addStaticPathResource(swaggerRestOperation);
      return;
    }

    dynamicPathOperationsList.add(swaggerRestOperation);
  }

  protected void addStaticPathResource(RestOperationMeta operation) {
    String httpMethod = operation.getHttpMethod();
    String path = operation.getAbsolutePath();
    OperationGroup group = staticPathOperations.get(path);
    if (group == null) {
      group = new OperationGroup();
      group.register(httpMethod, operation);
      staticPathOperations.put(path, group);
      return;
    }

    if (group.findValue(httpMethod) == null) {
      group.register(httpMethod, operation);
      return;
    }

    throw new ServiceCombException(
        String.format("operation with url %s, method %s is duplicated.", path, httpMethod));
  }

  public Map getStaticPathOperationMap() {
    return staticPathOperations;
  }

  public List getDynamicPathOperationList() {
    return dynamicPathOperationsList;
  }

  public void printPaths() {
    for (Entry entry : staticPathOperations.entrySet()) {
      OperationGroup operationGroup = entry.getValue();
      printPath(operationGroup.values());
    }

    printPath(getDynamicPathOperationList());
  }

  protected void printPath(Collection operations) {
    for (RestOperationMeta operation : operations) {
      LOGGER.info("Swagger mapped \"{[{}], method=[{}], produces={}}\" onto {}",
          operation.getAbsolutePath(),
          operation.getHttpMethod(),
          operation.getProduces(),
          operation.getOperationMeta().getMethod());
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy