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

vip.justlive.oxygen.web.WebPlugin Maven / Gradle / Ivy

There is a newer version: 3.0.10.1
Show newest version
/*
 * Copyright (C) 2020 the original author or authors.
 *
 * Licensed 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 vip.justlive.oxygen.web;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import vip.justlive.oxygen.core.Plugin;
import vip.justlive.oxygen.core.config.ConfigFactory;
import vip.justlive.oxygen.core.exception.Exceptions;
import vip.justlive.oxygen.core.net.http.HttpMethod;
import vip.justlive.oxygen.core.scan.ClassScannerPlugin;
import vip.justlive.oxygen.core.util.ClassUtils;
import vip.justlive.oxygen.core.util.Urls;
import vip.justlive.oxygen.ioc.IocPlugin;
import vip.justlive.oxygen.web.annotation.Mapping;
import vip.justlive.oxygen.web.annotation.Router;
import vip.justlive.oxygen.web.bind.ParamBinder;
import vip.justlive.oxygen.web.exception.ExceptionHandler;
import vip.justlive.oxygen.web.exception.ExceptionHandlerImpl;
import vip.justlive.oxygen.web.hook.WebHook;
import vip.justlive.oxygen.web.http.Parser;
import vip.justlive.oxygen.web.http.SessionStore;
import vip.justlive.oxygen.web.result.ResultHandler;
import vip.justlive.oxygen.web.router.AnnotationRouteHandler;
import vip.justlive.oxygen.web.router.Route;

/**
 * web插件
 *
 * @author wubo
 */
@Slf4j
public class WebPlugin implements Plugin {

  @Override
  public int order() {
    return Integer.MIN_VALUE + 60;
  }

  @Override
  public void start() {
    loadWebEnv();
    loadStaticRoute();
    loadAnnotationRouter();
    loadErrorHandler();
    vip.justlive.oxygen.web.router.Router.build();
  }

  @Override
  public void stop() {
    Context.PARSERS.clear();
    Context.BINDERS.clear();
    Context.HANDLERS.clear();
    Context.HOOKS.clear();
    vip.justlive.oxygen.web.router.Router.clear();
  }

  private void loadStaticRoute() {
    WebConf conf = ConfigFactory.load(WebConf.class);
    String[] paths = conf.getStaticPaths();
    if (paths == null) {
      return;
    }
    vip.justlive.oxygen.web.router.Router.staticRoute().prefix(conf.getStaticPrefix())
        .locations(Arrays.asList(paths)).cachingEnabled(conf.isViewCacheEnabled())
        .maxAge(conf.getStaticCache());
  }

  private void loadAnnotationRouter() {
    Set> routerClasses = ClassScannerPlugin.getTypesAnnotatedWith(Router.class);
    if (routerClasses == null || routerClasses.isEmpty()) {
      return;
    }

    for (Class clazz : routerClasses) {
      Router router = clazz.getAnnotation(Router.class);
      if (router == null) {
        continue;
      }
      parseRouter(router.value(), IocPlugin.beanStore().getBean(clazz.getName()));
    }
  }

  private void parseRouter(String rootPath, Object routerBean) {
    Class clazz = routerBean.getClass();
    Class actualClass = ClassUtils.getCglibActualClass(clazz);
    try {
      for (Method method : actualClass.getDeclaredMethods()) {
        if (!method.isAnnotationPresent(Mapping.class)) {
          continue;
        }
        Method requestMethod = clazz.getMethod(method.getName(), method.getParameterTypes());
        Mapping mapping = method.getAnnotation(Mapping.class);
        String routePath = Urls.concat(rootPath, mapping.value());
        Route route = vip.justlive.oxygen.web.router.Router.router().path(routePath);
        for (HttpMethod httpMethod : mapping.method()) {
          route.method(httpMethod);
        }
        route.handler(new AnnotationRouteHandler(routerBean, requestMethod, method));
      }
    } catch (NoSuchMethodException e) {
      throw Exceptions.wrap(e);
    }
  }

  private void loadErrorHandler() {
    ExceptionHandler handler = IocPlugin.beanStore().getBean(ExceptionHandler.class);
    if (handler != null) {
      if (log.isDebugEnabled()) {
        log.debug("loaded error handle of user {}", handler);
      }
    } else {
      handler = new ExceptionHandlerImpl();
      IocPlugin.beanStore().addBean(handler);
      if (log.isDebugEnabled()) {
        log.debug("loaded default error handle {}", handler);
      }
    }
  }

  private void loadWebEnv() {
    SessionStore sessionStore = IocPlugin.beanStore().getBean(SessionStore.class);
    if (sessionStore != null) {
      Context.SESSION_MANAGER.setStore(sessionStore);
    }
    Context.SESSION_MANAGER.setExpired(ConfigFactory.load(WebConf.class).getSessionExpired());

    Context.PARSERS.addAll(IocPlugin.beanStore().getCastBeanMap(Parser.class).values());
    Collections.sort(Context.PARSERS);

    Context.BINDERS.addAll(IocPlugin.beanStore().getCastBeanMap(ParamBinder.class).values());
    Collections.sort(Context.BINDERS);

    Context.HANDLERS.addAll(IocPlugin.beanStore().getCastBeanMap(ResultHandler.class).values());
    Collections.sort(Context.HANDLERS);

    Context.HOOKS.addAll(IocPlugin.beanStore().getCastBeanMap(WebHook.class).values());
    Collections.sort(Context.HOOKS);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy