001: package com.xoetrope.service.file;
002:
003: import java.io.BufferedReader;
004: import java.io.BufferedWriter;
005: import java.io.File;
006: import java.io.FileInputStream;
007: import java.io.FileOutputStream;
008: import java.io.IOException;
009: import java.io.InputStreamReader;
010: import java.io.OutputStreamWriter;
011: import net.xoetrope.optional.service.ServiceContext;
012:
013: import net.xoetrope.optional.service.ServiceProxy;
014: import net.xoetrope.optional.service.ServiceProxyArgs;
015: import net.xoetrope.optional.service.ServiceProxyException;
016:
017: /**
018: * This ServiceProxy class enables the saving and opening of files on a local
019: * filesystem.
020: *
021: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
022: * the GNU Public License (GPL), please see license.txt for more details. If
023: * you make commercial use of this software you must purchase a commercial
024: * license from Xoetrope.</p>
025: * <p> $Revision: 1.13 $</p>
026: */
027: public class FileSave extends ServiceProxy {
028: /**
029: * The mode of the call. Either ARG_VALUE_OPEN or ARG_VALUE_SAVE
030: */
031: public static final String ARG_NAME_MODE = "mode";
032:
033: /**
034: * The name of the file being written or opened
035: */
036: public static final String ARG_NAME_FILENAME = "filename";
037:
038: /**
039: * The path to the directory where the file is being written or opened
040: */
041: public static final String ARG_NAME_DIR = "filesavedirectory";
042:
043: /**
044: * The contents to be written if saving or returned if opening
045: */
046: public static final String ARG_NAME_CONTENTS = "contents";
047:
048: /**
049: * Set if a file with the same name exists and should be overwritten
050: */
051: public static final String ARG_NAME_OVERWRITE = "overwrite";
052:
053: /**
054: * Apply to the ARG_NAME_MODE parameter if opening the file
055: */
056: public static final String ARG_VALUE_OPEN = "open";
057:
058: /**
059: * Apply to the ARG_NAME_MODE parameter if saving the file
060: */
061: public static final String ARG_VALUE_SAVE = "save";
062:
063: /**
064: * Apply to the ARG_NAME_OVERWRITE parameter if the file is to be overwritten
065: */
066: public static final String ARG_VALUE_OVERWRITE_YES = "yes";
067:
068: public static final String ARG_ERROR_FILE_EXISTS = "The specified file already exists.";
069:
070: public static final String ARG_ERROR_CANNOT_OPEN_FILE = "Cannot open the specified file.";
071:
072: /**
073: * If the mode parameter is 'save' call the saveFile function. If the call
074: * mode parameter is 'open' open the file and return its contents
075: * @return the result of the call
076: * @param method the name of the service being called
077: * @param context The ServiceContext contain pass and return parameters
078: * @throws net.xoetrope.optional.service.ServiceProxyException Throw an exception if there is a problem with the call
079: */
080: public Object call(String method, ServiceContext context)
081: throws ServiceProxyException {
082: ServiceProxyArgs args = context.getArgs();
083: String mode = (String) args.getPassParam(ARG_NAME_MODE);
084: if (mode.compareTo(ARG_VALUE_SAVE) == 0) {
085: String filename = (String) args.getPassParam(ARG_NAME_DIR)
086: + File.separator
087: + (String) args.getPassParam(ARG_NAME_FILENAME);
088: saveFile(context, filename, (String) args
089: .getPassParam(ARG_NAME_CONTENTS), (String) args
090: .getPassParam(ARG_NAME_OVERWRITE));
091: } else if (mode.compareTo(ARG_VALUE_OPEN) == 0) {
092: String dir = (String) args.getPassParam(ARG_NAME_DIR);
093: String filename;
094: if (dir != null)
095: filename = dir + File.separator
096: + (String) args.getPassParam(ARG_NAME_FILENAME);
097: else
098: filename = (String) args
099: .getPassParam(ARG_NAME_FILENAME);
100: args.setReturnParam(ARG_NAME_CONTENTS, openFile(filename,
101: args));
102: }
103: return callNextProxy(method, context, null);
104: }
105:
106: /**
107: * Save the file to the path specified by the name parameter
108: * @param name The path to the file
109: * @param contents The contents to be written to the file
110: */
111: public void saveFile(ServiceContext context, String name,
112: String contents, String overwrite) {
113: if (overwrite == null
114: || overwrite.compareTo(ARG_VALUE_OVERWRITE_YES) != 0) {
115: File f = new File(name);
116: if (f.exists()) {
117: context.addError(ARG_ERROR_FILE_EXISTS);
118: return;
119: }
120: }
121: try {
122: FileOutputStream fos = new FileOutputStream(name);
123: OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF8");
124: BufferedWriter bw = new BufferedWriter(osw);
125: bw.write(contents);
126: bw.flush();
127: bw.close();
128: } catch (IOException ex) {
129: ex.printStackTrace();
130: }
131: }
132:
133: /**
134: * Open the file, read it and return its contents
135: * @param name The path to the file to be read
136: * @return The contents of the file
137: */
138: protected String openFile(String name, ServiceProxyArgs args) {
139: StringBuffer contents = new StringBuffer();
140: try {
141: FileInputStream fos = new FileInputStream(name);
142: InputStreamReader osw = new InputStreamReader(fos, "UTF8");
143: BufferedReader bw = new BufferedReader(osw);
144: String temp = bw.readLine();
145: while (temp != null) {
146: if (temp != null)
147: contents.append(temp);
148: temp = bw.readLine();
149: }
150: bw.close();
151: } catch (IOException ex) {
152: args.addError(ARG_ERROR_CANNOT_OPEN_FILE);
153: }
154: return contents.toString();
155: }
156: }
|