⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 confluencerpchandler.java

📁 xwiki 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * ===================================================================
 *
 * Copyright (c) 2003,2004 Ludovic Dubost, All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details, published at 
 * http://www.gnu.org/copyleft/lesser.html or in lesser.txt in the
 * root folder of this distribution.

 * Created by
 * User: Ludovic Dubost
 * Date: 17 juin 2004
 * Time: 11:24:01
 */
package com.xpn.xwiki.xmlrpc;

import com.xpn.xwiki.XWiki;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.web.XWikiEngineContext;
import com.xpn.xwiki.web.XWikiRequest;
import com.xpn.xwiki.web.XWikiResponse;
import com.xpn.xwiki.web.Utils;
import org.apache.commons.jrcs.rcs.Version;
import org.apache.velocity.VelocityContext;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

public class ConfluenceRpcHandler extends BaseRpcHandler {

    public class RemoteUser {

        public RemoteUser (String username, String ip) {
            this.ip = ip;
            this.username = username;
        }

        public String ip;
        public String username;
    }

    public ConfluenceRpcHandler(XWikiRequest request, XWikiResponse response, XWikiEngineContext econtext) {
        super(request, response, econtext);
    }

    public String login(String username, String password) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();
        if (username.equals("guest")) {
            String ip = context.getRequest().getRemoteAddr();
            String token = getValidationHash("guest", "guest", ip);
            getTokens(context).put(token, new RemoteUser("XWiki.XWikiGuest", ip));
            return token;
        }   else if (xwiki.getAuthService().authenticate(username, password, context)!=null) {
            String ip = context.getRequest().getRemoteAddr();
            String token = getValidationHash(username, password, ip);
            getTokens(context).put(token, new RemoteUser("XWiki." + username, ip));
            return token;
        } else
            return null;
    }

    private Hashtable getTokens(XWikiContext context) {
        Hashtable tokens = (Hashtable) context.getEngineContext().getAttribute("xmlrpc_tokens");
        if (tokens==null) {
            tokens = new Hashtable();
            context.getEngineContext().setAttribute("xmlrpc_tokens", tokens);
        }
        return tokens;
    }

    private String getValidationHash(String username, String password, String clientIP) {
        String validationKey = "xmlrpcapi";
        MessageDigest md5 = null;
        StringBuffer sbValueBeforeMD5 = new StringBuffer();

        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            System.out.println("Error: " + e);
        }

        try {
            sbValueBeforeMD5.append(username.toString());
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(password.toString());
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(clientIP.toString());
            sbValueBeforeMD5.append(":");
            sbValueBeforeMD5.append(validationKey.toString());

            String valueBeforeMD5 = sbValueBeforeMD5.toString();
            md5.update(valueBeforeMD5.getBytes());

            byte[] array = md5.digest();
            StringBuffer sb = new StringBuffer();
            for (int j = 0; j < array.length; ++j) {
                int b = array[j] & 0xFF;
                if (b < 0x10) sb.append('0');
                sb.append(Integer.toHexString(b));
            }
            String valueAfterMD5 = sb.toString();
            return valueAfterMD5;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void checkToken(String token, XWikiContext context) throws XWikiException {
        RemoteUser user = null;
        String ip = context.getRequest().getRemoteAddr();
        if (token != null)
             user = (RemoteUser)getTokens(context).get(token);
        if ((user==null)||(!user.ip.equals(ip))) {
            Object[] args = { token, ip };
            throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_TOKEN_INVALID,
                                     "Access Denied: authentification token {0} for ip {1} is invalid", null, args);
        }
        context.setUser(user.username);
    }

    public boolean logout(String token) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token
        checkToken(token, context);

        getTokens(context).remove(token);
        return true;
    }

    Hashtable getServerInfo(String token) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token
        checkToken(token, context);

        return null;
    }

    public Vector getSpaces(String token) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token
        checkToken(token, context);

        Vector spaces = new Vector();
        List webs = xwiki.search("select distinct doc.web from XWikiDocument doc", context);
        if (webs==null)
            return spaces;
        for (int i=0;i<webs.size();i++) {
            String web = (String)webs.get(i);
            SpaceSummary spacesum = new SpaceSummary(web, web, "http://127.0.0.1:9080/xwiki/bin/view/" + web + "/WebHome");
            spaces.add(spacesum.getHashtable());
        }
        return spaces;
    }

    public Hashtable getSpace(String token, String spaceKey) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token
        checkToken(token, context);

        XWikiDocument doc = new XWikiDocument(spaceKey, "WebHome");
        return (new Space(spaceKey, spaceKey, doc.getURL("view", context), spaceKey, "WebHome")).getHashtable();
    }

    public Vector getPages(String token, String spaceKey) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token
        checkToken(token, context);

        Vector pages = new Vector();
        List docs = xwiki.getStore().searchDocumentsNames("where doc.web='" + Utils.SQLFilter(spaceKey) + "'", context);
        if (docs==null)
            return null;
        for (int i=0;i<docs.size();i++) {
            String docname = (String)docs.get(i);
            XWikiDocument doc = xwiki.getDocument(docname, context);
            PageSummary pagesum = new PageSummary(doc, context);
            pages.add(pagesum.getHashtable());
        }
        return pages;
    }

    public Hashtable getPage(String token, String pageId) throws XWikiException {
        XWikiContext context = init();
        XWiki xwiki = context.getWiki();

        // Verify authentication token

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -