📄 defaultpacketprocessor.java
字号:
/* * * Copyright (C) GNU/GPL AVOIR 2008 * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU 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 General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */package org.avoir.realtime.plugins;import java.io.BufferedWriter;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;import java.util.ArrayList;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.avoir.realtime.common.util.XmlUtils;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.QName;import org.jivesoftware.openfire.PacketRouter;import org.jivesoftware.util.Base64;import org.w3c.dom.Document;import org.xmpp.packet.IQ;import org.xmpp.packet.JID;/** * * @author developer */public class DefaultPacketProcessor { private AvoirRealtimePlugin pl; private ArrayList<JID> jids; private PacketRouter packetRouter; public DefaultPacketProcessor(AvoirRealtimePlugin pl) { this.pl = pl; jids = pl.getUsers(false); packetRouter = pl.getPacketRouter(); } /** * open notepad. it contains the serialized * * @param packet * @param xdoc * @return */ public IQ openNotepadFile(IQ packet, Document xdoc) { String fileName = XmlUtils.readString(xdoc, "filename"); if (!fileName.endsWith(".np")) { fileName += ".np"; } String username = XmlUtils.readString(xdoc, "username"); IQ replyPacket = IQ.createResultIQ(packet); String xmlContents = Util.getContents(new File(Constants.FILES_DIR + "/notepads/" + username + "/" + fileName)); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document doc = documentBuilder.parse( new ByteArrayInputStream(xmlContents.getBytes(Constants.PREFERRED_ENCODING))); String filename = XmlUtils.readString(doc, "filename"); String content = XmlUtils.readString(doc, "file-content"); Element queryResult = DocumentHelper.createElement(QName.get("query", Constants.NAME_SPACE)); queryResult.addElement("packet-type").addText("notepad-open"); queryResult.addElement("filename").addText(filename); queryResult.addElement("file-content").addText(content); replyPacket.setChildElement(queryResult); } catch (Exception ex) { ex.printStackTrace(); } return replyPacket; } /** * save the notepad * @param packet * @param doc * @return */ public IQ saveNotepadFile(IQ packet, Document doc) { String fileName = XmlUtils.readString(doc, "filename"); String userName = XmlUtils.readString(doc, "username"); new File(Constants.FILES_DIR + "/notepads/" + userName).mkdirs(); String contents = packet.toXML(); Writer output = null; try { output = new BufferedWriter(new FileWriter(Constants.FILES_DIR + "/notepads/" + userName + "/" + fileName + ".np")); output.write(contents); } catch (IOException ex) { ex.printStackTrace(); } finally { try { output.close(); } catch (Exception ex) { ex.printStackTrace(); } } return null; } /** * save the image and return the updasted fileview * @param packet * @param filePath * @param doc * @return */ public IQ saveRealtimeImageFile(IQ packet, Document doc) { String filename = XmlUtils.readString(doc, "filename"); String path = Constants.FILES_DIR + "/images/" + filename; String username = XmlUtils.readString(doc, "username"); new File(Constants.FILES_DIR + "/images/" + username).mkdirs(); path = Constants.FILES_DIR + "/images/" + username + "/" + filename; String imageData = XmlUtils.readString(doc, "image-data"); try { FileChannel fc = new FileOutputStream(path).getChannel(); byte[] byteArray = Base64.decode(imageData); fc.write(ByteBuffer.wrap(byteArray)); fc.close(); } catch (Exception ex) { ex.printStackTrace(); } return getFileView(packet, "images", username); } public IQ getRealtimeImageFile(IQ packet, String imagePath, String type) { IQ replyPacket = IQ.createResultIQ(packet); Element queryResult = DocumentHelper.createElement(QName.get("query", Constants.NAME_SPACE)); queryResult.addElement("mode").addText("download-" + type + "-image"); String imageData = Base64.encodeFromFile(imagePath); StringBuilder sb = new StringBuilder(); sb.append("<image-data>").append(imageData).append("</image-data>"); sb.append("<image-path>").append(imagePath).append("</image-path>"); queryResult.addElement("content").addText(sb.toString()); replyPacket.setChildElement(queryResult); return replyPacket; } /** * Changes file access between private and public * @param packet * @param doc * @return */ public IQ changeAccess(IQ packet, Document doc) { String username = XmlUtils.readString(doc, "username"); String newAccessType = XmlUtils.readString(doc, "access"); String fileType = XmlUtils.readString(doc, "file-type"); String fileName = XmlUtils.readString(doc, "filename"); String oldFilePath = "."; String newFilePath = "."; if (newAccessType.equals("private")) { oldFilePath = Constants.FILES_DIR + "/" + fileType + "/" + fileName; newFilePath = Constants.FILES_DIR + "/" + fileType + "/" + username + "/" + fileName; new File(Constants.FILES_DIR + "/" + fileType + "/" + username).mkdirs(); } if (newAccessType.equals("public")) { oldFilePath = Constants.FILES_DIR + "/" + fileType + "/" + username + "/" + fileName; newFilePath = Constants.FILES_DIR + "/" + fileType + "/" + fileName; } //then do the moving File oldFile = new File(oldFilePath); File newFile = new File(newFilePath); oldFile.renameTo(newFile); pl.getRoomResourceManager().updateSlideShowRoomResourcePath(oldFilePath, newFilePath); //update view return getFileView(packet, fileType, username); } private String constructFileView(String fileType, String username) { StringBuilder sb = new StringBuilder(); File privateFileDir = new File(Constants.FILES_DIR + "/" + fileType + "/" + username + "/"); String[] privateList = privateFileDir.list(); sb.append("<fileview>"); if (privateList != null) { for (int i = 0; i < privateList.length; i++) { String path = Constants.FILES_DIR + "/" + fileType + "/" + username + "/" + privateList[i];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -