ServiceController.java 4.8 KB
Newer Older
M
min 已提交
1 2 3 4 5 6
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
N
nzomkxia 已提交
7
 * the License.  You may obtain a copy of the License at
M
min 已提交
8
 *
N
nzomkxia 已提交
9
 *     http://www.apache.org/licenses/LICENSE-2.0
M
min 已提交
10
 *
N
nzomkxia 已提交
11 12 13 14 15
 * 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.
M
min 已提交
16 17 18 19
 */

package org.apache.dubbo.admin.controller;

20
import com.google.gson.Gson;
N
nzomkxia 已提交
21
import org.apache.dubbo.admin.common.util.Constants;
22
import org.apache.dubbo.admin.common.util.ConvertUtil;
23 24
import org.apache.dubbo.admin.model.domain.Consumer;
import org.apache.dubbo.admin.model.domain.Provider;
N
nzomkxia 已提交
25 26 27 28
import org.apache.dubbo.admin.model.dto.ServiceDTO;
import org.apache.dubbo.admin.model.dto.ServiceDetailDTO;
import org.apache.dubbo.admin.service.ConsumerService;
import org.apache.dubbo.admin.service.ProviderService;
29
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
N
nzomkxia 已提交
30
import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
M
min 已提交
31
import org.springframework.beans.factory.annotation.Autowired;
N
nzomkxia 已提交
32 33 34 35 36
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
M
min 已提交
37

N
nzomkxia 已提交
38 39 40
import java.util.List;
import java.util.Map;
import java.util.Set;
M
min 已提交
41 42

@RestController
N
nzomkxia 已提交
43
@RequestMapping("/api/{env}")
M
min 已提交
44 45
public class ServiceController {

46 47
    private final ProviderService providerService;
    private final ConsumerService consumerService;
K
kezhenxu94 已提交
48
    private final Gson gson;
M
min 已提交
49 50

    @Autowired
51 52 53
    public ServiceController(ProviderService providerService, ConsumerService consumerService) {
        this.providerService = providerService;
        this.consumerService = consumerService;
K
kezhenxu94 已提交
54
        this.gson = new Gson();
55
    }
M
min 已提交
56

N
nzomkxia 已提交
57
    @RequestMapping( value = "/service", method = RequestMethod.GET)
N
nzomkxia 已提交
58 59
    public Set<ServiceDTO> searchService(@RequestParam String pattern,
                                         @RequestParam String filter,@PathVariable String env) {
wolf_fei's avatar
wolf_fei 已提交
60 61
        return providerService.getServiceDTOS(pattern, filter, env);
    }
M
min 已提交
62

N
nzomkxia 已提交
63
    @RequestMapping(value = "/service/{service}", method = RequestMethod.GET)
N
nzomkxia 已提交
64
    public ServiceDetailDTO serviceDetail(@PathVariable String service, @PathVariable String env) {
65
        service = service.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
N
nzomkxia 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78
        String group = null;
        String version = null;
        String interfaze = service;
        int i = interfaze.indexOf("/");
        if (i >= 0) {
            group = interfaze.substring(0, i);
            interfaze = interfaze.substring(i + 1);
        }
        i = interfaze.lastIndexOf(":");
        if (i >= 0) {
            version = interfaze.substring(i + 1);
            interfaze = interfaze.substring(0, i);
        }
79
        List<Provider> providers = providerService.findByService(service);
M
min 已提交
80

81
        List<Consumer> consumers = consumerService.findByService(service);
M
min 已提交
82

N
nzomkxia 已提交
83 84 85 86
        String application = null;
        if (providers != null && providers.size() > 0) {
            application = providers.get(0).getApplication();
        }
N
nzomkxia 已提交
87
        MetadataIdentifier identifier = new MetadataIdentifier(interfaze, version, group, Constants.PROVIDER_SIDE, application);
N
nzomkxia 已提交
88
        String metadata = providerService.getProviderMetaData(identifier);
N
nzomkxia 已提交
89
        ServiceDetailDTO serviceDetailDTO = new ServiceDetailDTO();
N
nzomkxia 已提交
90 91
        serviceDetailDTO.setConsumers(consumers);
        serviceDetailDTO.setProviders(providers);
N
nzomkxia 已提交
92 93 94 95
        if (metadata != null) {
            FullServiceDefinition serviceDefinition = gson.fromJson(metadata, FullServiceDefinition.class);
            serviceDetailDTO.setMetadata(serviceDefinition);
        }
N
nzomkxia 已提交
96 97
        serviceDetailDTO.setConsumers(consumers);
        serviceDetailDTO.setProviders(providers);
N
nzomkxia 已提交
98 99
        serviceDetailDTO.setService(service);
        serviceDetailDTO.setApplication(application);
N
nzomkxia 已提交
100
        return serviceDetailDTO;
M
min 已提交
101
    }
N
nzomkxia 已提交
102 103 104 105 106 107 108 109 110 111

    @RequestMapping(value = "/services", method = RequestMethod.GET)
    public Set<String> allServices(@PathVariable String env) {
        return providerService.findServices();
    }

    @RequestMapping(value = "/applications", method = RequestMethod.GET)
    public Set<String> allApplications(@PathVariable String env) {
        return providerService.findApplications();
    }
M
min 已提交
112
}