All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.dubbo.container.page.PageServlet Maven / Gradle / Ivy

There is a newer version: 3.3.2
Show newest version
/*
 * 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 com.alibaba.dubbo.container.page;

import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.URL;

/**
 * PageServlet
 */
public class PageServlet extends HttpServlet {

    protected static final Logger logger = LoggerFactory.getLogger(PageServlet.class);
    private static final long serialVersionUID = -8370312705453328501L;
    private static PageServlet INSTANCE;
    protected final Random random = new Random();
    protected final Map pages = new ConcurrentHashMap<>();
    protected final List menus = new ArrayList<>();

    public static PageServlet getInstance() {
        return INSTANCE;
    }

    public List getMenus() {
        return Collections.unmodifiableList(menus);
    }

    @Override
    public void init() throws ServletException {
        super.init();
        INSTANCE = this;
        String config = getServletConfig().getInitParameter("pages");
        Collection names;
        if (config != null && config.length() > 0) {
            names = Arrays.asList(Constants.COMMA_SPLIT_PATTERN.split(config));
        } else {
            names = ExtensionLoader.getExtensionLoader(PageHandler.class).getSupportedExtensions();
        }
        for (String name : names) {
            PageHandler handler =
                    ExtensionLoader.getExtensionLoader(PageHandler.class).getExtension(name);
            pages.put(ExtensionLoader.getExtensionLoader(PageHandler.class).getExtensionName(handler), handler);
            Menu menu = handler.getClass().getAnnotation(Menu.class);
            if (menu != null) {
                menus.add(handler);
            }
        }
        Collections.sort(menus, new MenuComparator());
    }

    @Override
    protected final void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected final void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        if (!response.isCommitted()) {
            PrintWriter writer = response.getWriter();
            String uri = request.getRequestURI();
            boolean isHtml = false;
            if (uri == null || uri.length() == 0 || "/".equals(uri)) {
                uri = "index";
                isHtml = true;
            } else {
                if (uri.startsWith("/")) {
                    uri = uri.substring(1);
                }
                if (uri.endsWith(".html")) {
                    uri = uri.substring(0, uri.length() - ".html".length());
                    isHtml = true;
                }
            }
            if (uri.endsWith("favicon.ico")) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return;
            }
            ExtensionLoader pageHandlerLoader = ExtensionLoader.getExtensionLoader(PageHandler.class);
            PageHandler pageHandler = pageHandlerLoader.hasExtension(uri) ? pageHandlerLoader.getExtension(uri) : null;
            if (isHtml) {
                writer.println("Dubbo");
                writer.println(
                        "");
                writer.println("");
            }
            if (pageHandler != null) {
                Page page = null;
                try {
                    String query = request.getQueryString();
                    page = pageHandler.handle(
                            URL.valueOf(request.getRequestURL().toString()
                                    + (query == null || query.length() == 0 ? "" : "?" + query)));
                } catch (Throwable t) {
                    logger.warn(t.getMessage(), t);
                    String msg = t.getMessage();
                    if (msg == null) {
                        msg = StringUtils.toString(t);
                    }
                    if (isHtml) {
                        writer.println("");
                        writer.println("");
                        writer.println("    ");
                        writer.println("        ");
                        writer.println("    ");
                        writer.println("");
                        writer.println("");
                        writer.println("    ");
                        writer.println("        ");
                        writer.println("    ");
                        writer.println("");
                        writer.println("
Error
"); writer.println(" " + msg.replace("<", "<").replace(">", "<").replace("\n", "
")); writer.println("
"); writer.println("
"); } else { writer.println(msg); } } if (page != null) { if (isHtml) { String nav = page.getNavigation(); if (nav == null || nav.length() == 0) { nav = ExtensionLoader.getExtensionLoader(PageHandler.class) .getExtensionName(pageHandler); nav = nav.substring(0, 1).toUpperCase() + nav.substring(1); } if (!"index".equals(uri)) { nav = "Home > " + nav; } writeMenu(request, writer, nav); writeTable(writer, page.getTitle(), page.getColumns(), page.getRows()); } else { if (page.getRows().size() > 0 && page.getRows().get(0).size() > 0) { writer.println(page.getRows().get(0).get(0)); } } } } else { if (isHtml) { writer.println(""); writer.println(""); writer.println(" "); writer.println(" "); writer.println(" "); writer.println(""); writer.println(""); writer.println(" "); writer.println(" "); writer.println(" "); writer.println(""); writer.println("
Error
"); writer.println(" Not found " + uri + " page. Please goto Home page."); writer.println("
"); writer.println("
"); } else { writer.println("Not found " + uri + " page."); } } if (isHtml) { writer.println(""); } writer.flush(); } } protected final void writeMenu(HttpServletRequest request, PrintWriter writer, String nav) { writer.println(""); writer.println(""); writer.println(" "); for (PageHandler handler : menus) { String uri = ExtensionLoader.getExtensionLoader(PageHandler.class).getExtensionName(handler); Menu menu = handler.getClass().getAnnotation(Menu.class); writer.println(" "); } writer.println(" "); writer.println(""); writer.println(""); writer.println(" "); writer.println(" "); writer.println(" "); writer.println(""); writer.println("
" + menu.name() + "
"); writer.println(nav); writer.println("
"); writer.println("
"); } protected final void writeTable(PrintWriter writer, String title, List columns, List> rows) { int n = random.nextInt(); int c = (columns == null ? (rows == null || rows.size() == 0 ? 0 : rows.get(0).size()) : columns.size()); int r = (rows == null ? 0 : rows.size()); writer.println(""); writer.println(""); writer.println(" "); writer.println(" "); writer.println(" "); if (columns != null && columns.size() > 0) { writer.println(" "); for (int i = 0; i < columns.size(); i++) { String col = columns.get(i); if (col.endsWith(":")) { col += " 0 && (tv.length < iv.length || tv.indexOf(iv) == -1)) { m = false; break; } } } document.getElementById('tr_" + n + "_' + i).style.display = (m ? '' : 'none');}\" style=\"width: 100%\" />"; } writer.println(" "); } writer.println(" "); } writer.println(""); if (rows != null && rows.size() > 0) { writer.println(""); int i = 0; for (Collection row : rows) { writer.println(" "); int j = 0; for (String col : row) { writer.println(" "); j++; } writer.println(" "); i++; } writer.println(""); } writer.println("
" + title + "
" + col + "
" + col + "
"); writer.println("
"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy