提交 96197120 编写于 作者: P peng-yongsheng

Provide GraphQL jetty servlet do get method.

上级 6b3aa98b
......@@ -16,7 +16,6 @@
*
*/
package org.apache.skywalking.apm.collector.server.jetty;
import com.google.gson.JsonElement;
......@@ -61,7 +60,7 @@ public abstract class JettyHandler extends HttpServlet implements ServerHandler
}
}
protected abstract JsonElement doPost(HttpServletRequest req) throws ArgumentsParseException;
protected abstract JsonElement doPost(HttpServletRequest req) throws ArgumentsParseException, IOException;
@Override
protected final void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
......@@ -150,7 +149,7 @@ public abstract class JettyHandler extends HttpServlet implements ServerHandler
}
private void reply(HttpServletResponse response, JsonElement resJson) throws IOException {
response.setContentType("text/json");
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.setStatus(HttpServletResponse.SC_OK);
......@@ -163,7 +162,7 @@ public abstract class JettyHandler extends HttpServlet implements ServerHandler
}
private void replyError(HttpServletResponse response, String errorMessage, int status) throws IOException {
response.setContentType("text/plain");
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.setStatus(status);
response.setHeader("error-message", errorMessage);
......
/*
* 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
*
* 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 org.apache.skywalking.apm.collector.ui.graphql;
import com.coxautodev.graphql.tools.SchemaParser;
import graphql.GraphQL;
import graphql.schema.GraphQLSchema;
import org.apache.skywalking.apm.collector.ui.graphql.alarm.AlarmQuery;
import org.apache.skywalking.apm.collector.ui.graphql.application.ApplicationQuery;
import org.apache.skywalking.apm.collector.ui.graphql.application.ConjecturalNode;
import org.apache.skywalking.apm.collector.ui.graphql.common.VisualUserNode;
import org.apache.skywalking.apm.collector.ui.graphql.config.ConfigMutation;
import org.apache.skywalking.apm.collector.ui.graphql.config.ConfigQuery;
import org.apache.skywalking.apm.collector.ui.graphql.overview.OverViewLayerQuery;
import org.apache.skywalking.apm.collector.ui.graphql.server.ServerQuery;
import org.apache.skywalking.apm.collector.ui.graphql.service.ServiceQuery;
import org.apache.skywalking.apm.collector.ui.graphql.trace.TraceQuery;
/**
* @author peng-yongsheng
*/
public class GraphQLCreator {
public GraphQL create() {
GraphQLSchema schema = SchemaParser.newParser()
.file("ui-graphql/alarm.graphqls")
.file("ui-graphql/application-layer.graphqls")
.file("ui-graphql/common.graphqls")
.file("ui-graphql/config.graphqls")
.file("ui-graphql/overview-layer.graphqls")
.file("ui-graphql/server-layer.graphqls")
.file("ui-graphql/service-layer.graphqls")
.file("ui-graphql/trace.graphqls")
.resolvers(new VersionQuery(), new VersionMutation(), new AlarmQuery(), new ApplicationQuery())
.resolvers(new OverViewLayerQuery(), new ServerQuery(), new ServiceQuery(), new TraceQuery())
.resolvers(new ConfigQuery(), new ConfigMutation())
.dictionary(ConjecturalNode.class, VisualUserNode.class)
.build()
.makeExecutableSchema();
return GraphQL.newGraphQL(schema).build();
}
}
......@@ -26,4 +26,20 @@ import java.util.List;
public class Alarm {
private List<AlarmItem> items;
private Integer count;
public List<AlarmItem> getItems() {
return items;
}
public void setItems(List<AlarmItem> items) {
this.items = items;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
......@@ -26,8 +26,10 @@ import org.apache.skywalking.apm.collector.ui.graphql.common.Pagination;
* @author peng-yongsheng
*/
public class AlarmQuery implements Query {
public Alarm loadAlertList(String keyword, AlarmType alarmType, Duration duration,
Pagination pagination) {
return null;
public Alarm loadAlertList(String keyword, AlarmType alarmType, Duration duration, Pagination pagination) {
Alarm alarm = new Alarm();
alarm.setCount(0);
return alarm;
}
}
......@@ -27,4 +27,27 @@ public class Duration {
private String end;
private Step step;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public Step getStep() {
return step;
}
public void setStep(Step step) {
this.step = step;
}
}
......@@ -25,4 +25,28 @@ public class Pagination {
private Integer pageNum;
private Integer pageSize;
private Boolean needTotal;
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Boolean getNeedTotal() {
return needTotal;
}
public void setNeedTotal(Boolean needTotal) {
this.needTotal = needTotal;
}
}
/*
* 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
*
* 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 org.apache.skywalking.apm.collector.ui.graphql;
import graphql.GraphQL;
import org.junit.Test;
/**
* @author peng-yongsheng
*/
public class GraphQLCreatorTestCase {
@Test
public void test() {
GraphQLCreator creator = new GraphQLCreator();
GraphQL graphQL = creator.create();
}
}
......@@ -33,6 +33,7 @@ import org.apache.skywalking.apm.collector.naming.service.NamingHandlerRegisterS
import org.apache.skywalking.apm.collector.server.Server;
import org.apache.skywalking.apm.collector.storage.StorageModule;
import org.apache.skywalking.apm.collector.ui.UIModule;
import org.apache.skywalking.apm.collector.ui.jetty.handler.GraphQLHandler;
import org.apache.skywalking.apm.collector.ui.jetty.handler.SegmentTopGetHandler;
import org.apache.skywalking.apm.collector.ui.jetty.handler.SpanGetHandler;
import org.apache.skywalking.apm.collector.ui.jetty.handler.TraceDagGetHandler;
......@@ -110,5 +111,7 @@ public class UIModuleJettyProvider extends ModuleProvider {
jettyServer.addHandler(new SpanGetHandler(getManager()));
jettyServer.addHandler(new TraceDagGetHandler(getManager()));
jettyServer.addHandler(new TraceStackGetHandler(getManager()));
jettyServer.addHandler(new GraphQLHandler());
}
}
/*
* 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
*
* 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 org.apache.skywalking.apm.collector.ui.jetty.handler;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQL;
import graphql.GraphQLError;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.skywalking.apm.collector.core.util.CollectionUtils;
import org.apache.skywalking.apm.collector.server.jetty.ArgumentsParseException;
import org.apache.skywalking.apm.collector.server.jetty.JettyHandler;
import org.apache.skywalking.apm.collector.ui.graphql.GraphQLCreator;
/**
* @author peng-yongsheng
*/
public class GraphQLHandler extends JettyHandler {
private final Gson gson = new Gson();
private final GraphQL graphQL;
private static final String QUERY = "query";
private static final String DATA = "data";
private static final String ERRORS = "errors";
public GraphQLHandler() {
GraphQLCreator creator = new GraphQLCreator();
this.graphQL = creator.create();
}
@Override public String pathSpec() {
return "/graphql";
}
@Override protected JsonElement doGet(HttpServletRequest req) throws ArgumentsParseException {
return execute(req.getParameter(QUERY));
}
@Override protected JsonElement doPost(HttpServletRequest req) throws ArgumentsParseException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
String line;
String request = "";
while ((line = reader.readLine()) != null) {
request += line;
}
return execute(request);
}
private JsonObject execute(String request) {
ExecutionInput executionInput = ExecutionInput.newExecutionInput().query(request).build();
ExecutionResult executionResult = graphQL.execute(executionInput);
Object data = executionResult.getData();
List<GraphQLError> errors = executionResult.getErrors();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty(DATA, data.toString());
if (CollectionUtils.isNotEmpty(errors)) {
String errorJsonStr = gson.toJson(errors, JsonArray.class);
JsonArray errorArray = gson.fromJson(errorJsonStr, JsonArray.class);
jsonObject.add(ERRORS, errorArray);
}
return jsonObject;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册