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

📄 xwikiservice.java

📁 xwiki 源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/**
 * ===================================================================
 *
 * 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: 27 mai 2004
 * Time: 09:48:22
 */
package com.xpn.xwiki;

import com.xpn.xwiki.api.Document;
import com.xpn.xwiki.atom.lifeblog.LifeblogServices;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
import com.xpn.xwiki.doc.XWikiLock;
import com.xpn.xwiki.objects.BaseObject;
import com.xpn.xwiki.objects.classes.BaseClass;
import com.xpn.xwiki.objects.classes.PropertyClass;
import com.xpn.xwiki.objects.meta.MetaClass;
import com.xpn.xwiki.objects.meta.PropertyMetaClass;
import com.xpn.xwiki.render.XWikiVelocityRenderer;
import com.xpn.xwiki.web.*;
import com.xpn.xwiki.pdf.impl.PdfExportImpl;
import com.xpn.xwiki.pdf.impl.PdfURLFactory;
import com.xpn.xwiki.pdf.api.PdfExport;
import com.xpn.xwiki.plugin.graphviz.GraphVizPlugin;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.VelocityContext;
import org.apache.ecs.html.P;

import java.io.*;
import java.text.ParseException;
import java.util.*;

public class XWikiService {

    private static final Log log = LogFactory.getLog(XWikiService.class);
    private static final long UPLOAD_DEFAULT_MAXSIZE = 10000000L;
    private static final long UPLOAD_DEFAULT_SIZETHRESHOLD = 100000L;

    private void sendRedirect(XWikiResponse response, String page) throws XWikiException {
        try {
            if (page!=null)
             response.sendRedirect(page);
        } catch (IOException e) {
            Object[] args = { page };
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                    XWikiException.ERROR_XWIKI_APP_REDIRECT_EXCEPTION,
                    "Exception while sending redirect to page {0}", e, args);
        }
    }

    /*
    *  Actions
    */

    public boolean actionLogout(XWikiContext context) throws XWikiException {
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();

        String redirect;
        redirect = context.getRequest().getParameter("xredirect");
        if ((redirect == null)||(redirect.equals("")))
            redirect = context.getURLFactory().createURL("Main", "WebHome", "view", context).toString();
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionDelattachment(XWikiContext context) throws XWikiException {
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();

        String path = request.getPathInfo();
        String filename = Utils.decode(path.substring(path.lastIndexOf("/")+1),context);
        XWikiAttachment attachment = null;

        if (request.getParameter("id")!=null) {
            int id = Integer.parseInt(request.getParameter("id"));
            attachment = (XWikiAttachment) doc.getAttachmentList().get(id);
        }
        else {
            attachment = doc.getAttachment(filename);
        }

        doc.deleteAttachment(attachment, context);
        // forward to attach page
        String redirect = Utils.getRedirect("attach", context);
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionUpload(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();

        String username = context.getUser();
        // Get the FileUpload Data
        DiskFileUpload fileupload = new DiskFileUpload();
        fileupload.setSizeMax(xwiki.getXWikiPreferenceAsLong("upload_maxsize", UPLOAD_DEFAULT_MAXSIZE, context));
        fileupload.setSizeThreshold((int)xwiki.getXWikiPreferenceAsLong("upload_sizethreshold", UPLOAD_DEFAULT_SIZETHRESHOLD, context));

        String tempdir = xwiki.Param("xwiki.upload.tempdir");
        if (tempdir!=null) {
            fileupload.setRepositoryPath(tempdir);
            (new File(tempdir)).mkdirs();
        }
        else
            fileupload.setRepositoryPath(".");
        List filelist = null;
        try {
            filelist = fileupload.parseRequest(request.getHttpServletRequest());
        } catch (FileUploadException e) {
            throw new XWikiException(XWikiException.MODULE_XWIKI_APP,
                    XWikiException.ERROR_XWIKI_APP_UPLOAD_PARSE_EXCEPTION,
                    "Exception while parsing uploaded file", e);
        }

        // I don't like it.. But this is the way
        // to get form elements..
        byte[] data = Utils.getContent(filelist, "filename");
        String filename = null;

        if (data!=null) {
            filename = new String(data);
        }

        // Get the file content
        data = Utils.getContent(filelist, "filepath");

        if (filename==null) {
            String fname = Utils.getFileName(filelist, "filepath");
            int i = fname.indexOf("\\");
            if (i==-1)
                i = fname.indexOf("/");
            filename = fname.substring(i+1);
        }


        // Read XWikiAttachment
        XWikiAttachment attachment = doc.getAttachment(filename);


        if (attachment==null) {
            attachment = new XWikiAttachment();
            doc.getAttachmentList().add(attachment);
        }
        attachment.setContent(data);
        attachment.setFilename(filename);

        // TODO: handle Author
        attachment.setAuthor(username);

        // Add the attachment to the document
        attachment.setDoc(doc);

        // Save the content and the archive
        doc.saveAttachmentContent(attachment, context);

        // forward to attach page
        sendRedirect(response, doc.getURL("attach", true, context));
        return false;
    }

    public boolean actionObjectremove(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        XWikiForm form = context.getForm();

        XWikiDocument olddoc = (XWikiDocument) doc.clone();
        String className = ((ObjectRemoveForm) form).getClassName();
        int classId = ((ObjectRemoveForm) form).getClassId();
        Vector objects = doc.getObjects(className);
        BaseObject object = (BaseObject)objects.get(classId);
        // Remove it from the object list
        objects.set(classId, null);
        doc.addObjectsToRemove(object);
        xwiki.saveDocument(doc, olddoc, context);

        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionObjectadd(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        ObjectAddForm oform = (ObjectAddForm) context.getForm();

        XWikiDocument olddoc = (XWikiDocument) doc.clone();
        String className = oform.getClassName();
        int nb = doc.createNewObject(className, context);

        BaseObject oldobject = doc.getObject(className, nb);
        BaseClass baseclass = oldobject.getxWikiClass(context);
        BaseObject newobject = (BaseObject) baseclass.fromMap(oform.getObject(className), oldobject);
        newobject.setNumber(oldobject.getNumber());
        newobject.setName(doc.getFullName());
        doc.setObject(className, nb, newobject);
        xwiki.saveDocument(doc, olddoc, context);

        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionCommentadd(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        ObjectAddForm oform = (ObjectAddForm) context.getForm();

        XWikiDocument olddoc = (XWikiDocument) doc.clone();
        String className = "XWiki.XWikiComments";
        int nb = doc.createNewObject(className, context);

        BaseObject oldobject = doc.getObject(className, nb);
        BaseClass baseclass = oldobject.getxWikiClass(context);
        BaseObject newobject = (BaseObject) baseclass.fromMap(oform.getObject(className), oldobject);
        newobject.setNumber(oldobject.getNumber());
        newobject.setName(doc.getFullName());
        doc.setObject(className, nb, newobject);
        xwiki.saveDocument(doc, olddoc, context);

        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionPropadd(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        XWikiForm form = context.getForm();

        XWikiDocument olddoc = (XWikiDocument) doc.clone();
        String propName = ((PropAddForm) form).getPropName();
        String propType = ((PropAddForm) form).getPropType();
        BaseClass bclass = doc.getxWikiClass();
        bclass.setName(doc.getFullName());
        if (bclass.get(propName)!=null) {
            // TODO: handle the error of the property already existing when we want to add a class property
        } else {
            MetaClass mclass = xwiki.getMetaclass();
            PropertyMetaClass pmclass = (PropertyMetaClass) mclass.get(propType);
            if (pmclass!=null) {
                PropertyClass pclass = (PropertyClass) pmclass.newObject();
                pclass.setObject(bclass);
                pclass.setName(propName);
                pclass.setPrettyName(propName);
                bclass.put(propName, pclass);
                xwiki.saveDocument(doc, olddoc, context);
            }
        }
        // forward to edit
        String redirect = Utils.getRedirect("edit", context);
        sendRedirect(response, redirect);
        return false;
    }

    public boolean actionPropupdate(XWikiContext context) throws XWikiException {
        XWiki xwiki = context.getWiki();
        XWikiRequest request = context.getRequest();
        XWikiResponse response = context.getResponse();
        XWikiDocument doc = context.getDoc();
        XWikiForm form = context.getForm();
        XWikiDocument olddoc = (XWikiDocument) doc.clone();

        // Prepare new class
        BaseClass bclass = doc.getxWikiClass();
        BaseClass bclass2 = (BaseClass)bclass.clone();
        bclass2.setFields(new HashMap());

        doc.setxWikiClass(bclass2);

        // Prepare a Map for field renames
        Map fieldsToRename = new HashMap();

⌨️ 快捷键说明

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