Search in sources :

Example 1 with PluginBean

use of com.akaxin.site.storage.bean.PluginBean in project openzaly by akaxincom.

the class ApiPluginService method page.

/**
 * <pre>
 * 获取插件扩展的展示页面,支持两种方式
 * 	1.非加密方式,此时扩展authkey不存在
 *  2.加密方式,此时扩展authkey存在
 * </pre>
 *
 * @param command
 * @return
 */
public CommandResponse page(Command command) {
    CommandResponse commandResponse = new CommandResponse().setAction(CommandConst.ACTION_RES);
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiPluginPageProto.ApiPluginPageRequest request = ApiPluginPageProto.ApiPluginPageRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        String pluginId = request.getPluginId();
        // /index || index.php || index.html
        String requestPage = request.getPage();
        String requestParams = request.getParams();
        LogUtils.requestDebugLog(logger, command, request.toString());
        Map<Integer, String> header = command.getHeader();
        String siteSessionId = header.get(CoreProto.HeaderKey.CLIENT_SOCKET_SITE_SESSION_ID_VALUE);
        String pluginRefere = header.get(CoreProto.HeaderKey.PLUGIN_CLIENT_REFERER_VALUE);
        if (StringUtils.isNoneEmpty(siteUserId, pluginId)) {
            PluginBean bean = SitePluginDao.getInstance().getPluginProfile(Integer.valueOf(pluginId));
            if (bean != null && bean.getApiUrl() != null) {
                String pageUrl = buildUrl(bean.getApiUrl(), requestPage, bean.getUrlPage());
                logger.debug("http request uri={}", pageUrl);
                PluginProto.ProxyPluginPackage.Builder packageBuilder = PluginProto.ProxyPluginPackage.newBuilder();
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE, siteUserId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.CLIENT_SITE_SESSION_ID_VALUE, siteSessionId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_ID_VALUE, pluginId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_TIMESTAMP_VALUE, String.valueOf(System.currentTimeMillis()));
                if (StringUtils.isNotEmpty(pluginRefere)) {
                    packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_REFERER_VALUE, pluginRefere);
                }
                if (StringUtils.isNotEmpty(requestParams)) {
                    packageBuilder.setData(requestParams);
                }
                byte[] httpContent = packageBuilder.build().toByteArray();
                String authKey = bean.getAuthKey();
                if (StringUtils.isNotEmpty(authKey)) {
                    // AES 加密整个proto,通过http传输给plugin
                    byte[] tsk = bean.getAuthKey().getBytes(CharsetCoding.ISO_8859_1);
                    byte[] enPostContent = AESCrypto.encrypt(tsk, httpContent);
                    httpContent = enPostContent;
                }
                byte[] httpResponse = ZalyHttpClient.getInstance().postBytes(pageUrl, httpContent);
                ApiPluginProxyProto.ApiPluginProxyResponse response = ApiPluginProxyProto.ApiPluginProxyResponse.newBuilder().setData(ByteString.copyFrom(httpResponse)).build();
                // byte[] httpResponse = ZalyHttpClient.getInstance().get(pageUrl);
                // ApiPluginPageProto.ApiPluginPageResponse response =
                // ApiPluginPageProto.ApiPluginPageResponse
                // .newBuilder().setData(ByteString.copyFrom(httpResponse)).build();
                commandResponse.setParams(response.toByteArray());
                errCode = ErrorCode2.SUCCESS;
            }
        } else {
            errCode = ErrorCode2.ERROR_PARAMETER;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : CommandResponse(com.akaxin.common.command.CommandResponse) ByteString(com.google.protobuf.ByteString) ErrorCode2(com.akaxin.common.constant.ErrorCode2) ApiPluginProxyProto(com.akaxin.proto.site.ApiPluginProxyProto) ApiPluginPageProto(com.akaxin.proto.site.ApiPluginPageProto) PluginBean(com.akaxin.site.storage.bean.PluginBean)

Example 2 with PluginBean

use of com.akaxin.site.storage.bean.PluginBean in project openzaly by akaxincom.

the class ApiPluginService method list.

/**
 * 分页获取扩展列表
 *
 * @param command
 * @return
 */
public CommandResponse list(Command command) {
    CommandResponse commandResponse = new CommandResponse().setAction(CommandConst.ACTION_RES);
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiPluginListProto.ApiPluginListRequest request = ApiPluginListProto.ApiPluginListRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        int pageNumber = request.getPageNumber();
        int pageSize = request.getPageSize();
        PluginProto.PluginPosition position = request.getPosition();
        String siteAdmin = SiteConfig.getSiteAdmin();
        LogUtils.requestDebugLog(logger, command, request.toString());
        // 先做首帧扩展
        if (PluginProto.PluginPosition.HOME_PAGE == position) {
            List<PluginBean> pluginList = null;
            if (StringUtils.isNotBlank(siteUserId) && siteUserId.equals(siteAdmin)) {
                pluginList = SitePluginDao.getInstance().getAdminPluginPageList(pageNumber, pageSize, position.getNumber());
            } else {
                pluginList = SitePluginDao.getInstance().getOrdinaryPluginPageList(pageNumber, pageSize, position.getNumber());
            }
            if (pluginList != null) {
                ApiPluginListProto.ApiPluginListResponse.Builder responseBuilder = ApiPluginListProto.ApiPluginListResponse.newBuilder();
                for (PluginBean bean : pluginList) {
                    responseBuilder.addPlugin(getPluginProfile(bean));
                }
                commandResponse.setParams(responseBuilder.build().toByteArray());
                errCode = ErrorCode2.SUCCESS;
            }
        } else {
            errCode = ErrorCode2.ERROR2_PLUGIN_STATUS;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : PluginProto(com.akaxin.proto.core.PluginProto) CommandResponse(com.akaxin.common.command.CommandResponse) ByteString(com.google.protobuf.ByteString) ApiPluginListProto(com.akaxin.proto.site.ApiPluginListProto) ErrorCode2(com.akaxin.common.constant.ErrorCode2) PluginBean(com.akaxin.site.storage.bean.PluginBean)

Example 3 with PluginBean

use of com.akaxin.site.storage.bean.PluginBean in project openzaly by akaxincom.

the class HttpPluginService method add.

/**
 * 申请添加好友
 *
 * @param command
 * @return
 */
public CommandResponse add(Command command) {
    CommandResponse commandResponse = new CommandResponse();
    ErrorCode2 errorCode = ErrorCode2.ERROR;
    try {
        HaiPluginAddProto.HaiPluginAddRequest request = HaiPluginAddProto.HaiPluginAddRequest.parseFrom(command.getParams());
        LogUtils.requestDebugLog(logger, command, request.toString());
        PluginBean bean = new PluginBean();
        bean.setName(request.getPlugin().getName());
        bean.setIcon(request.getPlugin().getIcon());
        bean.setUrlPage(request.getPlugin().getUrlPage());
        bean.setApiUrl(request.getPlugin().getApiUrl());
        bean.setAllowedIp(request.getPlugin().getAllowedIp());
        bean.setPosition(request.getPlugin().getPositionValue());
        bean.setDisplayMode(PluginProto.PluginDisplayMode.NEW_PAGE_VALUE);
        bean.setPermissionStatus(request.getPlugin().getPermissionStatusValue());
        bean.setAddTime(System.currentTimeMillis());
        // 随机生成64位的字符串
        bean.setAuthKey(StringHelper.generateRandomString(16));
        if (SitePluginDao.getInstance().addPlugin(bean)) {
            errorCode = ErrorCode2.SUCCESS;
        }
    } catch (Exception e) {
        errorCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errorCode);
}
Also used : HaiPluginAddProto(com.akaxin.proto.plugin.HaiPluginAddProto) ErrorCode2(com.akaxin.common.constant.ErrorCode2) CommandResponse(com.akaxin.common.command.CommandResponse) PluginBean(com.akaxin.site.storage.bean.PluginBean)

Example 4 with PluginBean

use of com.akaxin.site.storage.bean.PluginBean in project openzaly by akaxincom.

the class PluginSession method getPluginAuthKey.

public String getPluginAuthKey(String sitePluginId) {
    try {
        int pluginId = Integer.valueOf(sitePluginId);
        PluginBean bean = pluginDao.getPluginProfile(pluginId);
        if (bean != null) {
            return bean.getAuthKey();
        }
    } catch (Exception e) {
        logger.error(StringHelper.format("{} get plugin authKey error pluginId={}", AkxProject.PLN, sitePluginId), e);
    }
    return null;
}
Also used : PluginBean(com.akaxin.site.storage.bean.PluginBean)

Example 5 with PluginBean

use of com.akaxin.site.storage.bean.PluginBean in project openzaly by akaxincom.

the class ApiPluginService method proxy.

/**
 * <pre>
 * 	代理前台客户端中扩展的请求
 * 		1.界面请求后台一般使用http请求
 * 		2.使用tcp代理,代替http请求
 * </pre>
 *
 * @param command
 * @return
 */
public CommandResponse proxy(Command command) {
    CommandResponse commandResponse = new CommandResponse().setAction(CommandConst.ACTION_RES);
    ErrorCode2 errCode = ErrorCode2.ERROR;
    try {
        ApiPluginProxyProto.ApiPluginProxyRequest request = ApiPluginProxyProto.ApiPluginProxyRequest.parseFrom(command.getParams());
        String siteUserId = command.getSiteUserId();
        String pluginId = request.getPluginId();
        String requestApi = request.getApi();
        String requestParams = request.getParams();
        LogUtils.requestDebugLog(logger, command, request.toString());
        Map<Integer, String> header = command.getHeader();
        String siteSessionId = header.get(CoreProto.HeaderKey.CLIENT_SOCKET_SITE_SESSION_ID_VALUE);
        String pluginRefere = header.get(CoreProto.HeaderKey.PLUGIN_CLIENT_REFERER_VALUE);
        if (!StringUtils.isAnyBlank(siteUserId, pluginId, requestApi)) {
            PluginBean bean = SitePluginDao.getInstance().getPluginProfile(Integer.valueOf(pluginId));
            if (bean != null && bean.getApiUrl() != null) {
                String pluginUrl = this.buildUrl(bean.getApiUrl(), requestApi, null);
                logger.debug("action={} pluginId={} api={} url={} params={}", command.getAction(), pluginId, requestApi, pluginUrl, requestParams);
                PluginProto.ProxyPluginPackage.Builder packageBuilder = PluginProto.ProxyPluginPackage.newBuilder();
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.CLIENT_SITE_USER_ID_VALUE, siteUserId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.CLIENT_SITE_SESSION_ID_VALUE, siteSessionId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_ID_VALUE, pluginId);
                packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_TIMESTAMP_VALUE, String.valueOf(System.currentTimeMillis()));
                if (StringUtils.isNotEmpty(pluginRefere)) {
                    packageBuilder.putPluginHeader(PluginProto.PluginHeaderKey.PLUGIN_REFERER_VALUE, pluginRefere);
                }
                if (StringUtils.isNotEmpty(requestParams)) {
                    packageBuilder.setData(requestParams);
                }
                byte[] httpContent = packageBuilder.build().toByteArray();
                String authKey = bean.getAuthKey();
                if (StringUtils.isNotEmpty(authKey)) {
                    // AES 加密整个proto,通过http传输给plugin
                    // byte[] tsk = AESCrypto.generateTSKey(bean.getAuthKey());
                    byte[] tsk = bean.getAuthKey().getBytes(CharsetCoding.ISO_8859_1);
                    byte[] enPostContent = AESCrypto.encrypt(tsk, httpContent);
                    httpContent = enPostContent;
                }
                byte[] httpResponse = ZalyHttpClient.getInstance().postBytes(pluginUrl, httpContent);
                ApiPluginProxyProto.ApiPluginProxyResponse response = ApiPluginProxyProto.ApiPluginProxyResponse.newBuilder().setData(ByteString.copyFrom(httpResponse)).build();
                // httpResposne,callback方法的回调方法参数
                commandResponse.setParams(response.toByteArray());
                errCode = ErrorCode2.SUCCESS;
            }
        } else {
            errCode = ErrorCode2.ERROR_PARAMETER;
        }
    } catch (Exception e) {
        errCode = ErrorCode2.ERROR_SYSTEMERROR;
        LogUtils.requestErrorLog(logger, command, e);
    }
    return commandResponse.setErrCode2(errCode);
}
Also used : CommandResponse(com.akaxin.common.command.CommandResponse) ByteString(com.google.protobuf.ByteString) ErrorCode2(com.akaxin.common.constant.ErrorCode2) ApiPluginProxyProto(com.akaxin.proto.site.ApiPluginProxyProto) PluginBean(com.akaxin.site.storage.bean.PluginBean)

Aggregations

PluginBean (com.akaxin.site.storage.bean.PluginBean)11 CommandResponse (com.akaxin.common.command.CommandResponse)7 ErrorCode2 (com.akaxin.common.constant.ErrorCode2)7 ByteString (com.google.protobuf.ByteString)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 ApiPluginProxyProto (com.akaxin.proto.site.ApiPluginProxyProto)2 ArrayList (java.util.ArrayList)2 PluginProto (com.akaxin.proto.core.PluginProto)1 HaiPluginAddProto (com.akaxin.proto.plugin.HaiPluginAddProto)1 HaiPluginListProto (com.akaxin.proto.plugin.HaiPluginListProto)1 HaiPluginProfileProto (com.akaxin.proto.plugin.HaiPluginProfileProto)1 HaiPluginUpdateProto (com.akaxin.proto.plugin.HaiPluginUpdateProto)1 ApiPluginListProto (com.akaxin.proto.site.ApiPluginListProto)1 ApiPluginPageProto (com.akaxin.proto.site.ApiPluginPageProto)1