forked from camel-tooling/camel-language-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptionParamURIInstance.java
129 lines (112 loc) · 5.12 KB
/
OptionParamURIInstance.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* 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
* the License. You may obtain a copy of the License at
*
* https://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 com.github.cameltooling.lsp.internal.instancemodel;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import org.apache.camel.catalog.CamelCatalog;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.TextDocumentItem;
import com.github.cameltooling.lsp.internal.catalog.model.ComponentModel;
import com.github.cameltooling.lsp.internal.catalog.model.EndpointOptionModel;
import com.github.cameltooling.lsp.internal.catalog.util.KameletsCatalogManager;
import com.github.cameltooling.lsp.internal.settings.SettingsManager;
import io.fabric8.camelk.v1alpha1.Kamelet;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
/**
* For a Camel URI "timer:timerName?delay=10s", it represents "delay=10s"
*
*/
public class OptionParamURIInstance extends CamelUriElementInstance {
public static final String INVALID_URI_OPTION = "The parameter %s is not a known parameter of the component. Check if it is written correctly and is supported by the used Camel version (see pom.xml).";
private OptionParamKeyURIInstance key;
private OptionParamValueURIInstance value;
private CamelURIInstance camelURIInstance;
public OptionParamURIInstance(CamelURIInstance camelURIInstance, String optionParam, int startPosition, int endPosition) {
super(startPosition, endPosition);
this.camelURIInstance = camelURIInstance;
String[] splittedParams = optionParam.split("=");
String keyName = splittedParams[0];
key = new OptionParamKeyURIInstance(this, splittedParams[0], startPosition, startPosition + keyName.length());
if (splittedParams.length > 1) {
value = new OptionParamValueURIInstance(this, splittedParams[1], startPosition + keyName.length() + 1, endPosition);
} else if(optionParam.endsWith("=")){
value = new OptionParamValueURIInstance(this, null, startPosition + keyName.length() + 1, endPosition);
}
}
public OptionParamKeyURIInstance getKey() {
return key;
}
public OptionParamValueURIInstance getValue() {
return value;
}
public CamelUriElementInstance getSpecificElement(int position) {
if(key.isInRange(position)) {
return key;
} else {
return value;
}
}
@Override
public CompletableFuture<List<CompletionItem>> getCompletions(CompletableFuture<CamelCatalog> camelCatalog, int positionInCamelUri, TextDocumentItem docItem, SettingsManager settingsManager, KameletsCatalogManager kameletsCatalogManager) {
return CompletableFuture.completedFuture(Collections.emptyList());
}
public String getComponentName() {
return camelURIInstance.getComponentName();
}
public boolean isProducer() {
return camelURIInstance.isProducer();
}
@Override
public String getDescription(ComponentModel componentModel, KameletsCatalogManager kameletCatalogManager) {
String keyName = getKey().getKeyName();
EndpointOptionModel model = componentModel.getEndpointOption(keyName);
if(model != null) {
return model.getDescription();
} else if (ComponentNameConstants.COMPONENT_NAME_KAMELET.equalsIgnoreCase(componentModel.getScheme())) {
JSONSchemaProps prop = getKameletPropertyByKeyName(kameletCatalogManager, keyName);
if (prop != null) {
return prop.getDescription();
}
return String.format(INVALID_URI_OPTION, keyName);
} else {
Optional<EndpointOptionModel> apiProperty = findAvailableApiProperties(componentModel).stream()
.filter(endpointOptionModel -> endpointOptionModel.getName().equals(keyName))
.findAny();
return apiProperty.isPresent() ? apiProperty.get().getDescription() : String.format(INVALID_URI_OPTION, keyName);
}
}
@Override
public CamelURIInstance getCamelUriInstance() {
return camelURIInstance;
}
private JSONSchemaProps getKameletPropertyByKeyName(KameletsCatalogManager kameletCatalogManager, String keyName) {
Optional<String> kameletTemplateId = this.getCamelUriInstance().getComponentAndPathUriElementInstance().getPathParams()
.stream()
.filter(pathParam -> pathParam.getPathParamIndex() == 0)
.map(PathParamURIInstance::getValue).findAny();
if(kameletTemplateId.isPresent()) {
List<Kamelet> kamelets = kameletCatalogManager.getCatalog().getKameletsByName(kameletTemplateId.get());
if (!kamelets.isEmpty()) {
Kamelet kamelet = kamelets.get(0);
return kamelet.getSpec().getDefinition().getProperties().get(keyName);
}
}
return null;
}
}