com.aspectran.demo.terminal.TransletInterpreter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aspectran-demo Show documentation
Show all versions of aspectran-demo Show documentation
Aspectran module showcasing a demo application
/*
* Copyright (c) 2008-2025 The Aspectran Project
*
* 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 com.aspectran.demo.terminal;
import com.aspectran.core.activity.InstantActivitySupport;
import com.aspectran.core.activity.Translet;
import com.aspectran.core.component.bean.annotation.Bean;
import com.aspectran.core.component.bean.annotation.Component;
import com.aspectran.core.component.bean.annotation.RequestToGet;
import com.aspectran.core.component.bean.annotation.RequestToPost;
import com.aspectran.core.component.bean.annotation.Transform;
import com.aspectran.core.context.asel.token.Token;
import com.aspectran.core.context.asel.token.TokenParser;
import com.aspectran.core.context.rule.DescriptionRule;
import com.aspectran.core.context.rule.ItemRule;
import com.aspectran.core.context.rule.ItemRuleMap;
import com.aspectran.core.context.rule.TransletRule;
import com.aspectran.core.context.rule.type.FormatType;
import com.aspectran.core.context.rule.type.TokenType;
import com.aspectran.utils.StringUtils;
import com.aspectran.utils.annotation.jsr305.NonNull;
import com.aspectran.utils.json.JsonWriter;
import com.aspectran.utils.logging.Logger;
import com.aspectran.utils.logging.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Component("/terminal")
@Bean("transletInterpreter")
public class TransletInterpreter extends InstantActivitySupport {
private final Logger logger = LoggerFactory.getLogger(TransletInterpreter.class);
private final String COMMANDS_PREFIX = "/commands/";
@RequestToGet("/query/@{_translet_}")
@Transform(format = FormatType.TEXT, contentType = "application/json")
public void query(@NonNull Translet translet) throws IOException {
String requestName = translet.getAttribute("_translet_");
if (StringUtils.isEmpty(requestName)) {
return;
}
requestName = COMMANDS_PREFIX + requestName;
TransletRule transletRule = getActivityContext().getTransletRuleRegistry().getTransletRule(requestName);
if (transletRule == null) {
if (logger.isDebugEnabled()) {
logger.debug("No translet " + requestName);
}
new JsonWriter(translet.getResponseAdapter().getWriter())
.beginObject()
.name("translet").value(null)
.name("request").value(null)
.name("response").value(null)
.endObject();
return;
}
ItemRuleMap parameterItemRuleMap = transletRule.getRequestRule().getParameterItemRuleMap();
ItemRuleMap attributeItemRuleMap = transletRule.getRequestRule().getAttributeItemRuleMap();
JsonWriter jsonWriter = new JsonWriter(translet.getResponseAdapter().getWriter())
.beginObject()
.name("translet")
.beginObject()
.name("name").value(transletRule.getName());
if (transletRule.getDescriptionRule() != null) {
String description = DescriptionRule.render(transletRule.getDescriptionRule(), getCurrentActivity());
jsonWriter.name("description").value(description);
}
jsonWriter.endObject()
.name("request")
.beginObject();
if (parameterItemRuleMap != null) {
jsonWriter.name("parameters")
.beginObject()
.name("items").value(toListForItems(parameterItemRuleMap.values()))
.name("tokens").value(toListForTokens(parameterItemRuleMap.values()))
.endObject();
}
if (attributeItemRuleMap != null) {
jsonWriter.name("attributes")
.beginObject()
.name("items").value(toListForItems(attributeItemRuleMap.values()))
.name("tokens").value(toListForTokens(attributeItemRuleMap.values()))
.endObject();
}
jsonWriter.endObject()
.name("response")
.beginObject();
if (transletRule.getResponseRule().getResponse() != null) {
jsonWriter.name("contentType").value(transletRule.getResponseRule().getResponse().getContentType());
}
jsonWriter.endObject().endObject();
}
@RequestToPost("/exec/@{_translet_}")
public void execute(@NonNull Translet translet) {
String requestName = translet.getAttribute("_translet_");
if (StringUtils.isEmpty(requestName)) {
return;
}
try {
requestName = COMMANDS_PREFIX + requestName;
instantActivity(requestName);
} catch (Exception e) {
logger.error("Failed to execute translet: " + requestName, e);
}
}
@NonNull
private List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy