Search in sources :

Example 1 with User

use of com.alibaba.dubbo.registry.common.domain.User in project dubbo by alibaba.

the class Restful method execute.

public void execute(Map<String, Object> context) throws Throwable {
    if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
        User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
        currentUser = user;
        operator = user.getUsername();
        role = user.getRole();
        context.put(WebConstants.CURRENT_USER_KEY, user);
    }
    operatorAddress = (String) context.get("request.remoteHost");
    context.put("operator", operator);
    context.put("operatorAddress", operatorAddress);
    context.put("currentRegistry", currentRegistry);
    String httpMethod = (String) context.get("request.method");
    String method = (String) context.get("_method");
    String contextPath = (String) context.get("request.contextPath");
    context.put("rootContextPath", new RootContextPath(contextPath));
    // 分析Method
    if (method == null || method.length() == 0) {
        String id = (String) context.get("id");
        if (id == null || id.length() == 0) {
            method = "index";
        } else {
            method = "show";
        }
    }
    if ("index".equals(method)) {
        if ("post".equalsIgnoreCase(httpMethod)) {
            method = "create";
        }
    } else if ("show".equals(method)) {
        if ("put".equalsIgnoreCase(httpMethod) || "post".equalsIgnoreCase(httpMethod)) {
            // 因表单不能提交PUT请求,用POST代替
            method = "update";
        } else if ("delete".equalsIgnoreCase(httpMethod)) {
            // 因表单不能提交DELETE请求,用参数代替
            method = "delete";
        }
    }
    context.put("_method", method);
    try {
        Method m = null;
        try {
            m = getClass().getMethod(method, new Class<?>[] { Map.class });
        } catch (NoSuchMethodException e) {
            for (Method mtd : getClass().getMethods()) {
                if (Modifier.isPublic(mtd.getModifiers()) && mtd.getName().equals(method)) {
                    m = mtd;
                    break;
                }
            }
            if (m == null) {
                throw e;
            }
        }
        if (m.getParameterTypes().length > 2) {
            throw new IllegalStateException("Unsupport restful method " + m);
        } else if (m.getParameterTypes().length == 2 && (m.getParameterTypes()[0].equals(Map.class) || !m.getParameterTypes()[1].equals(Map.class))) {
            throw new IllegalStateException("Unsupport restful method " + m);
        }
        Object r;
        if (m.getParameterTypes().length == 0) {
            r = m.invoke(this, new Object[0]);
        } else {
            Object value;
            Class<?> t = m.getParameterTypes()[0];
            if (Map.class.equals(t)) {
                value = context;
            } else if (isPrimitive(t)) {
                String id = (String) context.get("id");
                value = convertPrimitive(t, id);
            } else if (t.isArray() && isPrimitive(t.getComponentType())) {
                String id = (String) context.get("id");
                String[] ids = id == null ? new String[0] : id.split("[.+]+");
                value = Array.newInstance(t.getComponentType(), ids.length);
                for (int i = 0; i < ids.length; i++) {
                    Array.set(value, i, convertPrimitive(t.getComponentType(), ids[i]));
                }
            } else {
                value = t.newInstance();
                for (Method mtd : t.getMethods()) {
                    if (Modifier.isPublic(mtd.getModifiers()) && mtd.getName().startsWith("set") && mtd.getParameterTypes().length == 1) {
                        String p = mtd.getName().substring(3, 4).toLowerCase() + mtd.getName().substring(4);
                        Object v = context.get(p);
                        if (v == null) {
                            if ("operator".equals(p)) {
                                v = operator;
                            } else if ("operatorAddress".equals(p)) {
                                v = (String) context.get("request.remoteHost");
                            }
                        }
                        if (v != null) {
                            try {
                                mtd.invoke(value, new Object[] { CompatibleTypeUtils.compatibleTypeConvert(v, mtd.getParameterTypes()[0]) });
                            } catch (Throwable e) {
                                logger.warn(e.getMessage(), e);
                            }
                        }
                    }
                }
            }
            if (m.getParameterTypes().length == 1) {
                r = m.invoke(this, new Object[] { value });
            } else {
                r = m.invoke(this, new Object[] { value, context });
            }
        }
        if (m.getReturnType() == boolean.class || m.getReturnType() == Boolean.class) {
            context.put("rundata.layout", "redirect");
            context.put("rundata.target", "redirect");
            context.put("success", r == null || ((Boolean) r).booleanValue());
            if (context.get("redirect") == null) {
                context.put("redirect", getDefaultRedirect(context, method));
            }
        } else if (m.getReturnType() == String.class) {
            String redirect = (String) r;
            if (redirect == null) {
                redirect = getDefaultRedirect(context, method);
            }
            if (context.get("chain") != null) {
                context.put("rundata.layout", "home");
                context.put("rundata.target", "home");
            } else {
                context.put("rundata.redirect", redirect);
            }
        } else {
            context.put("rundata.layout", method);
            context.put("rundata.target", context.get("rundata.target") + "/" + method);
        }
    } catch (Throwable e) {
        if (e instanceof InvocationTargetException) {
            throw ((InvocationTargetException) e).getTargetException();
        }
    //            if (e instanceof InvocationTargetException) {
    //                e = ((InvocationTargetException) e).getTargetException();
    //            }
    //            logger.warn(e.getMessage(), e);
    //            context.put("rundata.layout", "redirect");
    //            context.put("rundata.target", "redirect");
    //            context.put("success", false);
    //            context.put("exception", e);
    //            context.put("redirect", getDefaultRedirect(context, method));
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) RootContextPath(com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath) Map(java.util.Map)

Example 2 with User

use of com.alibaba.dubbo.registry.common.domain.User in project dubbo by alibaba.

the class Restful method execute.

public void execute(Map<String, Object> context) throws Exception {
    Result result = new Result();
    if (request.getParameter("url") != null) {
        url = URL.valueOf(URL.decode(request.getParameter("url")));
    }
    if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
        User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
        currentUser = user;
        operator = user.getUsername();
        role = user.getRole();
        context.put(WebConstants.CURRENT_USER_KEY, user);
    }
    operatorAddress = (String) context.get("clientid");
    if (operatorAddress == null || operatorAddress.isEmpty()) {
        operatorAddress = (String) context.get("request.remoteHost");
    }
    context.put("operator", operator);
    context.put("operatorAddress", operatorAddress);
    String jsonResult = null;
    try {
        result = doExecute(context);
        result.setStatus("OK");
    } catch (IllegalArgumentException t) {
        result.setStatus("ERROR");
        result.setCode(3);
        result.setMessage(t.getMessage());
    }//        }
     catch (Throwable t) {
        result.setStatus("ERROR");
        result.setCode(1);
        result.setMessage(t.getMessage());
    }
    response.setContentType("application/javascript");
    ServletOutputStream os = response.getOutputStream();
    try {
        jsonResult = JSON.toJSONString(result);
        os.print(jsonResult);
    } catch (Exception e) {
        response.setStatus(500);
        os.print(e.getMessage());
    } finally {
        os.flush();
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 3 with User

use of com.alibaba.dubbo.registry.common.domain.User in project incubator-dubbo-ops by apache.

the class AuthorizationValve method invoke.

public void invoke(PipelineContext pipelineContext) throws Exception {
    if (logger.isInfoEnabled()) {
        logger.info("AuthorizationValve of uri: " + request.getRequestURI());
    }
    String uri = request.getRequestURI();
    String contextPath = request.getContextPath();
    if (contextPath != null && contextPath.length() > 0 && !"/".equals(contextPath)) {
        uri = uri.substring(contextPath.length());
    }
    if (uri.equals(logout)) {
        if (!isLogout()) {
            setLogout(true);
            showLoginForm();
        } else {
            setLogout(false);
            response.sendRedirect(contextPath == null || contextPath.length() == 0 ? "/" : contextPath);
        }
        return;
    }
    User user = null;
    String authType = null;
    String authorization = request.getHeader("Authorization");
    if (authorization != null && authorization.length() > 0) {
        int i = authorization.indexOf(' ');
        if (i >= 0) {
            authType = authorization.substring(0, i);
            String authPrincipal = authorization.substring(i + 1);
            if (BASIC_CHALLENGE.equalsIgnoreCase(authType)) {
                user = loginByBase(authPrincipal);
            } else if (DIGEST_CHALLENGE.equalsIgnoreCase(authType)) {
                user = loginByDigest(authPrincipal);
            }
        }
    }
    if (user == null || user.getUsername() == null || user.getUsername().length() == 0) {
        showLoginForm();
        pipelineContext.breakPipeline(1);
    }
    if (user != null && StringUtils.isNotEmpty(user.getUsername())) {
        request.getSession().setAttribute(WebConstants.CURRENT_USER_KEY, user);
        pipelineContext.invokeNext();
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User)

Example 4 with User

use of com.alibaba.dubbo.registry.common.domain.User in project incubator-dubbo-ops by apache.

the class ServicePrivilegeCheckValve method invoke.

public void invoke(PipelineContext pipelineContext) throws Exception {
    User user = (User) request.getSession().getAttribute(WebConstants.CURRENT_USER_KEY);
    invokeCheckServicePrivilege(user);
    pipelineContext.invokeNext();
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User)

Example 5 with User

use of com.alibaba.dubbo.registry.common.domain.User in project incubator-dubbo-ops by apache.

the class Restful method execute.

public void execute(Map<String, Object> context) throws Exception {
    Result result = new Result();
    if (request.getParameter("url") != null) {
        url = URL.valueOf(URL.decode(request.getParameter("url")));
    }
    if (context.get(WebConstants.CURRENT_USER_KEY) != null) {
        User user = (User) context.get(WebConstants.CURRENT_USER_KEY);
        currentUser = user;
        operator = user.getUsername();
        role = user.getRole();
        context.put(WebConstants.CURRENT_USER_KEY, user);
    }
    operatorAddress = (String) context.get("clientid");
    if (operatorAddress == null || operatorAddress.isEmpty()) {
        operatorAddress = (String) context.get("request.remoteHost");
    }
    context.put("operator", operator);
    context.put("operatorAddress", operatorAddress);
    String jsonResult = null;
    try {
        result = doExecute(context);
        result.setStatus("OK");
    } catch (IllegalArgumentException t) {
        result.setStatus("ERROR");
        result.setCode(3);
        result.setMessage(t.getMessage());
    }// }
     catch (Throwable t) {
        result.setStatus("ERROR");
        result.setCode(1);
        result.setMessage(t.getMessage());
    }
    response.setContentType("application/javascript");
    ServletOutputStream os = response.getOutputStream();
    try {
        jsonResult = JSON.toJSONString(result);
        os.print(jsonResult);
    } catch (Exception e) {
        response.setStatus(500);
        os.print(e.getMessage());
    } finally {
        os.flush();
    }
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) ServletOutputStream(javax.servlet.ServletOutputStream)

Aggregations

User (com.alibaba.dubbo.registry.common.domain.User)24 RootContextPath (com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath)4 PrintWriter (java.io.PrintWriter)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Method (java.lang.reflect.Method)2 Map (java.util.Map)2 ServletOutputStream (javax.servlet.ServletOutputStream)2