Search in sources :

Example 1 with RootContextPath

use of com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath 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 RootContextPath

use of com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath in project dubbo by alibaba.

the class Menu method execute.

public void execute(HttpSession session, Context context, CookieParser parser) {
    User user = (User) session.getAttribute(WebConstants.CURRENT_USER_KEY);
    if (user != null)
        context.put("operator", user.getUsername());
    RootContextPath rootContextPath = new RootContextPath(request.getContextPath());
    context.put("rootContextPath", rootContextPath);
    if (!context.containsKey("bucLogoutAddress")) {
        context.put("bucLogoutAddress", rootContextPath.getURI("logout"));
    }
    if (!context.containsKey("helpUrl")) {
        context.put("helpUrl", "http://code.alibabatech.com/wiki/display/dubbo");
    }
    context.put(WebConstants.CURRENT_USER_KEY, user);
    context.put("language", parser.getString("locale"));
    context.put("registryServerSync", registryServerSync);
}
Also used : User(com.alibaba.dubbo.registry.common.domain.User) RootContextPath(com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath)

Example 3 with RootContextPath

use of com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath in project dubbo by alibaba.

the class Index method execute.

public void execute(Context context) {
    Set<String> applications = new HashSet<String>();
    Set<String> services = new HashSet<String>();
    List<Provider> pList = new ArrayList<Provider>();
    try {
        pList = providerService.findAll();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    for (Provider p : pList) {
        applications.add(p.getApplication());
        services.add(p.getService());
    }
    List<Consumer> cList = new ArrayList<Consumer>();
    try {
        cList = consumerService.findAll();
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }
    for (Consumer c : cList) {
        applications.add(c.getApplication());
        services.add(c.getService());
    }
    context.put("rootContextPath", new RootContextPath(request.getContextPath()));
    context.put("services", services.size());
    context.put("providers", pList.size());
    context.put("consumers", cList.size());
    context.put("applications", applications.size());
}
Also used : Consumer(com.alibaba.dubbo.registry.common.domain.Consumer) RootContextPath(com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Provider(com.alibaba.dubbo.registry.common.domain.Provider)

Example 4 with RootContextPath

use of com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath in project dubbo by alibaba.

the class Error_404 method execute.

public void execute(Map<String, Object> context) throws Throwable {
    String contextPath = (String) context.get("request.contextPath");
    context.put("rootContextPath", new RootContextPath(contextPath));
}
Also used : RootContextPath(com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath)

Example 5 with RootContextPath

use of com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath in project dubbo by alibaba.

the class Error_other method execute.

public void execute(Map<String, Object> context) throws Throwable {
    String contextPath = (String) context.get("request.contextPath");
    context.put("rootContextPath", new RootContextPath(contextPath));
}
Also used : RootContextPath(com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath)

Aggregations

RootContextPath (com.alibaba.dubbo.governance.web.common.pulltool.RootContextPath)5 User (com.alibaba.dubbo.registry.common.domain.User)2 Consumer (com.alibaba.dubbo.registry.common.domain.Consumer)1 Provider (com.alibaba.dubbo.registry.common.domain.Provider)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1