Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[linkis-application-manager] replace jersey with spring web #1002

Merged
merged 1 commit into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ wds.linkis.server.mybatis.BasePackage=com.webank.wedatasphere.linkis.manager.dao

##Spring
spring.server.port=9101
spring.spring.mvc.servlet.path=/api/rest_s/v1
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
@Bean
public WebServerFactoryCustomizer<JettyServletWebServerFactory> jettyFactoryCustomizer() {
return new WebServerFactoryCustomizer<JettyServletWebServerFactory>() {
@Override
public void customize(JettyServletWebServerFactory jettyServletWebServerFactory) {
jettyServletWebServerFactory.addServerCustomizers(new JettyServerCustomizer() {
@Override
public void customize(Server server) {
Handler[] childHandlersByClass = server.getChildHandlersByClass(WebAppContext.class);
final WebAppContext webApp = (WebAppContext) childHandlersByClass[0];
Expand All @@ -185,6 +187,9 @@ public void customize(Server server) {
filterHolder.setInitParameter("forceEncoding", "true");
webApp.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class));
BDPJettyServerHelper.setupRestApiContextHandler(webApp);

//set servletHolder for spring restful api
BDPJettyServerHelper.setupSpringRestApiContextHandler(webApp);
if(ServerConfiguration.BDP_SERVER_SOCKET_MODE().getValue()) {
BDPJettyServerHelper.setupControllerServer(webApp);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import org.eclipse.jetty.server.session.SessionHandler
import org.eclipse.jetty.servlet.{DefaultServlet, FilterHolder, ServletContextHandler, ServletHolder}
import org.eclipse.jetty.webapp.WebAppContext
import org.glassfish.jersey.servlet.ServletContainer
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext
import org.springframework.web.servlet.DispatcherServlet

import scala.collection.mutable

Expand Down Expand Up @@ -77,6 +79,28 @@ private[linkis] object BDPJettyServerHelper extends Logging {
webApp.addFilter(filterHolder, restfulPath, EnumSet.allOf(classOf[DispatcherType]))
}

def setupSpringRestApiContextHandler(webApp: ServletContextHandler) {
val context = new AnnotationConfigWebApplicationContext
//val CONFIG_LOCATION = "com.webank.wedatasphere.linkis.manager.am"
val CONFIG_LOCATION = ""
context.setConfigLocation(CONFIG_LOCATION);
val serlvet = new DispatcherServlet(context)
val servletHolder = new ServletHolder(serlvet)

servletHolder.setName("springrestful")
servletHolder.setForcedPath("springrestful")

val p = BDP_SERVER_SPRING_RESTFUL_URI.getValue
val restfulPath = if(p.endsWith("/*")) p
else if(p.endsWith("/")) p + "*"
else p + "/*"

webApp.addServlet(servletHolder, restfulPath)
val filterHolder = new FilterHolder(getSecurityFilter())
webApp.addFilter(filterHolder, restfulPath, EnumSet.allOf(classOf[DispatcherType]))
webApp.setSessionHandler(new SessionHandler)

}
def setupControllerServer(webApp: ServletContextHandler): ControllerServer = {
if(controllerServer != null) return controllerServer
synchronized {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,22 @@ package com.webank.wedatasphere.linkis.server

import java.util

import com.webank.wedatasphere.linkis.server.Message.getCurrentHttpRequest
import javax.servlet.http.HttpServletRequest
import javax.ws.rs.Path
import javax.ws.rs.core.Response
import javax.xml.bind.annotation.XmlRootElement
import org.apache.commons.lang.StringUtils
import org.apache.commons.lang.exception.ExceptionUtils
import org.reflections.ReflectionUtils
import org.springframework.web.context.request.{RequestContextHolder, ServletRequestAttributes}


@XmlRootElement(name = "message")
class Message(private var method: String,
private var status: Int = 0, //-1 no login, 0 success, 1 error, 2 validate failed, 3 auth failed, 4 warning
private var message: String,
private var data: util.HashMap[String, Object] = new util.HashMap[String, Object]) {
private var data: util.HashMap[String, Object] = new util.HashMap[String, Object]) {
def this() = this(null, 0, null)
def << (key: String, value: Any): Message = {
data.put(key, value.asInstanceOf[AnyRef])
Expand Down Expand Up @@ -57,26 +60,41 @@ class Message(private var method: String,
def setData(data: util.HashMap[String, Object]): Unit = this.data = data
def getData = data

// def isSuccess = status == 0
// def isError = status != 0
// def isSuccess = status == 0
// def isError = status != 0

override def toString = s"Message($getMethod, $getStatus, $getData)"
}

object Message {
def apply(method: String = null, status: Int = 0, message: String = null,
data: util.HashMap[String, Object] = new util.HashMap[String, Object]): Message = {
if(StringUtils.isEmpty(method)) {
Thread.currentThread().getStackTrace.find(_.getClassName.toLowerCase.endsWith("restfulapi")).foreach { stack =>
val clazz = ReflectionUtils.forName(stack.getClassName)
val path = clazz.getAnnotation(classOf[Path]).value()
clazz.getDeclaredMethods.find(m => m.getName == stack.getMethodName && m.getAnnotation(classOf[Path]) != null)
.foreach { m =>
val path1 = m.getAnnotation(classOf[Path]).value()
var method = if(path.startsWith("/")) path else "/" + path
if(method.endsWith("/")) method = method.substring(0, method.length - 1)
method = if(path1.startsWith("/")) "/api" + method + path1 else "/api" + method + "/" + path1
return new Message(method, status, message, data)
if (StringUtils.isEmpty(method)) {
Thread.currentThread().getStackTrace.find(_.getClassName.toLowerCase.endsWith("restfulapi")).foreach {
stack => {
val clazz = ReflectionUtils.forName(stack.getClassName)
var annotation = clazz.getAnnotation(classOf[Path])
var path = ""
if (annotation != null) {
path = clazz.getAnnotation(classOf[Path]).value()
clazz.getDeclaredMethods.find(m => m.getName == stack.getMethodName && m.getAnnotation(classOf[Path]) != null)
.foreach { m =>
val path1 = m.getAnnotation(classOf[Path]).value()
var method = if (path.startsWith("/")) path else "/" + path
if (method.endsWith("/")) method = method.substring(0, method.length - 1)
method = if (path1.startsWith("/")) "/api" + method + path1 else "/api" + method + "/" + path1
return new Message(method, status, message, data)
}
} else {
val httpRequest:HttpServletRequest=getCurrentHttpRequest
if(httpRequest!=null){
val path1=httpRequest.getPathInfo;
var method = if (path.startsWith("/")) path else "/" + path
if (method.endsWith("/")) method = method.substring(0, method.length - 1)
method = if (path1.startsWith("/")) "/api" + method + path1 else "/api" + method + "/" + path1
return new Message(method, status, message, data)
}
}
}
}
}
Expand Down Expand Up @@ -106,6 +124,15 @@ object Message {

implicit def response(message: Message): String = BDPJettyServerHelper.gson.toJson(message)

def getCurrentHttpRequest: HttpServletRequest = {
val requestAttributes = RequestContextHolder.getRequestAttributes
if (requestAttributes.isInstanceOf[ServletRequestAttributes]) {
val request = requestAttributes.asInstanceOf[ServletRequestAttributes].getRequest
return request
}
null
}

def noLogin(msg: String, t: Throwable): Message = {
val message = Message(status = -1)
message.setMessage(msg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ object ServerConfiguration extends Logging{

val BDP_SERVER_SERVER_CONTEXT_PATH = CommonVars("wds.linkis.server.context.path", "/")
val BDP_SERVER_RESTFUL_URI = CommonVars("wds.linkis.server.restful.uri", "/api/rest_j/" + BDP_SERVER_VERSION)
val BDP_SERVER_SPRING_RESTFUL_URI = CommonVars("wds.linkis.server.restful.uri", "/api/rest_s/" + BDP_SERVER_VERSION)
val BDP_SERVER_USER_URI = CommonVars("wds.linkis.server.user.restful.uri", "/api/rest_j/" + BDP_SERVER_VERSION + "/user")
val BDP_SERVER_RESTFUL_LOGIN_URI = CommonVars("wds.linkis.server.user.restful.login.uri", new File(BDP_SERVER_USER_URI.getValue, "login").getPath)
val BDP_SERVER_SECURITY_SSL_URI = CommonVars("wds.linkis.server.user.security.ssl.uri", new File(BDP_SERVER_USER_URI.getValue, "publicKey").getPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@

package com.webank.wedatasphere.linkis.manager.am.restful;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.netflix.discovery.converters.Auto;
import com.webank.wedatasphere.linkis.common.ServiceInstance;
import com.webank.wedatasphere.linkis.manager.am.conf.AMConfiguration;
import com.webank.wedatasphere.linkis.manager.am.converter.DefaultMetricsConverter;
Expand All @@ -35,42 +31,31 @@
import com.webank.wedatasphere.linkis.manager.common.entity.node.EMNode;
import com.webank.wedatasphere.linkis.manager.label.builder.factory.LabelBuilderFactory;
import com.webank.wedatasphere.linkis.manager.label.builder.factory.LabelBuilderFactoryContext;
import com.webank.wedatasphere.linkis.manager.label.builder.factory.StdLabelBuilderFactory;
import com.webank.wedatasphere.linkis.manager.label.entity.Label;
import com.webank.wedatasphere.linkis.manager.label.entity.UserModifiable;
import com.webank.wedatasphere.linkis.manager.label.exception.LabelErrorException;
import com.webank.wedatasphere.linkis.manager.label.service.NodeLabelService;
import com.webank.wedatasphere.linkis.server.BDPJettyServerHelper;
import com.webank.wedatasphere.linkis.server.Message;
import com.webank.wedatasphere.linkis.server.security.SecurityFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.json4s.JsonAST;
import org.json4s.JsonUtil;
import org.json4s.jackson.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import scala.util.parsing.json.JSON;
import scala.util.parsing.json.JSONObject;
import scala.util.parsing.json.JSONObject$;
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;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.*;
import java.util.stream.Collectors;


@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Component
@Path("/linkisManager")
@RequestMapping(path = "/linkisManager", produces = {"application/json"})
@RestController
public class EMRestfulApi {

@Autowired
Expand All @@ -88,12 +73,11 @@ public class EMRestfulApi {


//todo add healthInfo
@GET
@Path("/listAllEMs")
public Response listAllEMs(@Context HttpServletRequest req,
@QueryParam("instance") String instance,
@QueryParam("nodeHealthy") String nodeHealthy,
@QueryParam("owner" )String owner) throws AMErrorException {
@RequestMapping(path = "/listAllEMs", method = RequestMethod.GET)
public Message listAllEMs(HttpServletRequest req,
@RequestParam(value = "instance",required = false) String instance,
@RequestParam(value = "nodeHealthy",required = false) String nodeHealthy,
@RequestParam(value = "owner",required = false)String owner) throws AMErrorException {
String userName = SecurityFilter.getLoginUsername(req);
String[] adminArray = AMConfiguration.GOVERNANCE_STATION_ADMIN().getValue().split(",");
if(adminArray != null && !Arrays.asList(adminArray).contains(userName)){
Expand All @@ -113,24 +97,23 @@ public Response listAllEMs(@Context HttpServletRequest req,
if(CollectionUtils.isNotEmpty(allEMVoFilter3) && !StringUtils.isEmpty(owner)){
allEMVoFilter3 = (ArrayList<EMNodeVo>) allEMVoFilter3.stream().filter(em ->{return em.getOwner().equalsIgnoreCase(owner);}).collect(Collectors.toList());
}
return Message.messageToResponse(Message.ok().data("EMs", allEMVoFilter3));
return Message.ok().data("EMs", allEMVoFilter3);
}

@GET
@Path("/listAllECMHealthyStatus")
public Response listAllNodeHealthyStatus(@Context HttpServletRequest req, @QueryParam("onlyEditable") Boolean onlyEditable){
@RequestMapping(path = "/listAllECMHealthyStatus", method = RequestMethod.GET)
public Message listAllNodeHealthyStatus( HttpServletRequest req,
@RequestParam(value = "onlyEditable",required = false) Boolean onlyEditable){
NodeHealthy[] nodeHealthy = NodeHealthy.values();
if(onlyEditable){
nodeHealthy = new NodeHealthy[]{NodeHealthy.Healthy, NodeHealthy.UnHealthy,
NodeHealthy.WARN, NodeHealthy.StockAvailable, NodeHealthy.StockUnavailable};
}
return Message.messageToResponse(Message.ok().data("nodeHealthy", nodeHealthy));
return Message.ok().data("nodeHealthy", nodeHealthy);
}

@PUT
@Path("/modifyEMInfo")
@RequestMapping(path = "/modifyEMInfo", method = RequestMethod.PUT)
@Transactional(rollbackFor = Exception.class)
public Response modifyEMInfo(@Context HttpServletRequest req, JsonNode jsonNode) throws AMErrorException, LabelErrorException {
public Message modifyEMInfo( HttpServletRequest req, JsonNode jsonNode) throws AMErrorException, LabelErrorException {
String username = SecurityFilter.getLoginUsername(req);
String[] adminArray = AMConfiguration.GOVERNANCE_STATION_ADMIN().getValue().split(",");
if(adminArray != null && !Arrays.asList(adminArray).contains(username)){
Expand Down Expand Up @@ -176,7 +159,7 @@ public Response modifyEMInfo(@Context HttpServletRequest req, JsonNode jsonNode)
nodeLabelService.updateLabelsToNode(serviceInstance, newLabelList);
logger.info("success to update label of instance: " + serviceInstance.getInstance());
}
return Message.messageToResponse(Message.ok("修改EM信息成功"));
return Message.ok("修改EM信息成功");
}

}
Loading