📄 vpptransactionwrapper.java
字号:
/* * ==================================================================== * The Vovida Software License, Version 1.0 * * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The names "VOCAL", "Vovida Open Communication Application Library", * and "Vovida Open Communication Application Library (VOCAL)" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact vocal@vovida.org. * * 4. Products derived from this software may not be called "VOCAL", nor * may "VOCAL" appear in their name, without prior written * permission of Vovida Networks, Inc. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * ==================================================================== * * This software consists of voluntary contributions made by Vovida * Networks, Inc. and many individuals on behalf of Vovida Networks, * Inc. For more information on Vovida Networks, Inc., please see * <http://www.vovida.org/>. * */package vocal.comm;import java.io.IOException;import java.io.FileNotFoundException;import java.io.BufferedReader;import java.io.StringReader;import java.net.ConnectException;import javax.swing.*;/** * This class provides GUI-friendly versions of the public methods of * VPPTransaction, catching exceptions they throw and displaying * them in dialog boxes. */public class VPPTransactionWrapper{ VPPTransaction wrappee; public VPPTransactionWrapper(String server, int port) throws VPPException { // will throw a VPPLowLevelException if it cannot connect // properly to the server wrappee = new VPPTransaction(server, port); } /** * Requests authorization from the server. * Authorization must be granted before the server will * honor other requests from this connection. * @param username login id * @param password password to authenticate for this login id * @return true if authentication granted */ public int authenticate(String username, String password) throws VPPException { return wrappee.doAuth(username, password); } /** * Sends a GET request to the server. <p> * @param group string giving the path to the file to get. * @param filename name of the file, must include extension * @return a string containing the respone * throws VPPNoSuchFileException on error (which may not be an error * if this is being used to check for the existence of the file) or * some other VPP error (such as malformed header). Be sure to check * that the error is a NoSuchFileException if you are using this to * check for the existence of a file */ public String doGet(String group, String filename) throws VPPException { String stringToReturn = ""; stringToReturn = wrappee.doGet(group, filename); return stringToReturn; } /** * Sends a request to delete an item from the server. <p> * */ public void doDel(String group, String item) throws VPPException { wrappee.doDel(group, item); } /** * Perform the "PUT" operation. * @param group the destination of the data * @param filename name of the file holding the data. Must include * extension. * @param entityToPut the data to PUT. * @exception VPPException on failure */ public void doPut(String group, String filename, String entityToPut) throws VPPException { wrappee.doPut(group, filename, entityToPut); } public void doSubPut(String group, String filename, String entityToPut) throws VPPException { wrappee.doSubPut(group, filename, entityToPut); } /** * Perform the "PUT" operation. * @param group the destination of the data * @param filename name of the file holding the data. Must include * extension. * @param entityToPut the data to PUT. * @param destination flag to determine if one should put to the * server or to a file. The options are WRITE_TO_FILE or WRITE_TO_PSERVER * @exception VPPException on failure */ public void doPut(String group, String filename, String entityToPut, int destination) throws VPPException { wrappee.doPut(group, filename, entityToPut, destination); } /** * A more generic request method which returns the response wrapped in a * BufferedReader instead of a string. * @param type string which specifies the type of request to perform (Eg, * GET, DELETE. * @param arg1 first argument to the request type (Eg, group) * @param arg2 second argument to the request type (Eg, filename) * If the user sends a GET they may get VPPNoSuchFileException, * else, they may get VPPPermissionException. As always, may throw * other VPP errors for error conditions. IOException can be thrown * if there is a problem with the BufferedReader */ public BufferedReader request(String type, String arg1, String arg2) throws VPPException { BufferedReader response = null; response = wrappee.request(type, arg1, arg2); return response; } /** * A request that will be sent through exactly as is. * @return the response as a String */ public String request(String type, String requestString) throws VPPException { String response = null; response = wrappee.sendRequest(type, requestString, ""); return response; } public String requestString(String type, String arg1, String arg2) throws VPPException { String response = null; response = wrappee.requestString(type, arg1, arg1); return response; } /** * Displays a dialog which shows the text of an exception. */ public static void showException(Exception exception, String message) { StringBuffer explanation = new StringBuffer(message); explanation.append("\n\n"); if (exception instanceof VPPException) { explanation.append("There was a problem communicating with" + "the pServer.\n\nThe Output was:\n\n"); } else if (exception instanceof IOException) { explanation.append("There was an input/output problem.\n\n" + "Please try your operation again.\n\n" + "Here is the exception thrown:\n\n"); } explanation.append(exception.getMessage()); JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, explanation.toString(), "Error", JOptionPane.ERROR_MESSAGE); } /** * Displays a dialog which shows the text of an exception, but handles * the various VPP cases more specifically. */ public static void showVPPException(VPPException exception, String message) { StringBuffer explanation = new StringBuffer(message); explanation.append("\n\n"); if (exception instanceof VPPLowLevelException) { explanation.append("A low level failure occured while " + "communicating with the pServer.\n\n" + "The exception thrown was: \n\n"); } else if (exception instanceof VPPMalformedMsgException) { explanation.append("The transaction failed due to " + "malformed messages being passed\n" + "between the client and server.\n\n" + "The problem reported was was: \n\n"); } else if (exception instanceof VPPNoConnException) { explanation.append("The transaction failed because " + "the GUI is not connected to the server." + "\n\n" + "The problem reported was was: \n\n"); } else if (exception instanceof VPPNoSuchFileException) { explanation.append("The transaction failed because " + "the requested file could not be found " + "or read.\n\n" + "The problem reported was was: \n\n"); } else if (exception instanceof VPPAuthException) { explanation.append("The transaction failed because " + "permission was denied. Even if the\n" + "write shows on the server, it likely " + "did not make it to the server.\n\n" + "The problem reported was was: \n\n"); } else if (exception instanceof VPPPermissionException) { explanation.append("Authorization Failed\n\n"); } explanation.append(exception.getMessage()); JFrame frame = new JFrame(); JOptionPane.showMessageDialog(frame, explanation.toString(), "Error", JOptionPane.ERROR_MESSAGE); } /** * Close the streams and socket for the underlying VPPTransaction * once you are finished using the connection. * Can throw a low level exception. */ public void close() throws VPPException { wrappee.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -