|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
com.sas.util.ContentServer2
public class ContentServer2
A class for sending content to a browser (either through applet.showDocument or
directly invoking the browser) or directly to an application.
It communicates to a browser through a socket; for an application, it writes
the data to a file first.
Example usage:
ContentServer2.export(pdfString.getBytes(), "application/pdf", "", "", "pdf"); or ContentServer2.export("1, 2, 3, 4".getBytes(), "application/x-comma-separated-values", "", "", "csv");or a more elaborate version:
class SomeClass { protected final static String _excelArgs = " /r "; // read-only protected final static String _fileExt = "csv"; protected final static String EXCEL = "Excel"; // assumes that something sets a system property called "Excel" protected static String getExcelEXE() { return System.getProperty(EXCEL, "C:\\Microsoft Office\\Office\\Excel.exe"); } public static void export(StaticTableInterface table) { if (!ContentServer2.isExportAllowed() || table == null) return; // set type for spreadsheet ContentServer2.export(com.sas.table.TableWriter.getCSVAsByteArray(table), "application/x-comma-separated-values", getExcelEXE(), _excelArgs, _fileExt); } }
Field Summary | |
---|---|
protected java.lang.String |
_application
The path of the application if using sendToApplication |
protected java.lang.String |
_browser
The path of the browser if using sendToBrowserApp |
protected static boolean |
_canWriteToDisk
Are we allowed to write to disk? |
protected static boolean |
_checked
Have we checked if we can export and by what method? |
protected byte[] |
_content
The content to be served as a byte array |
protected static int |
_counter
How many exports have we done? |
protected static java.lang.String |
_echoURL
The url of the echocgi.exe that we found |
protected static java.lang.String[] |
_echoURLs
The possible URLs for echocgi.exe - by default either Windows or Unix at /sasweb/cgi-bin/echocgi |
protected java.lang.String |
_extension
The extension of the file (eg csv) |
protected java.lang.String |
_fileName
The file name of the file to be Written to disk if using sendToApplication Requested from our internal mini-web server if using sendToBrowserServerSocket or sendToBrowserApp |
protected static boolean |
_isInApplet
Are we in an applet but not in appletviewer? |
protected int |
_localPort
The local port of the server socket to listen on if we use a server socket, otherwise -1 |
protected int |
_maxPort
The maximum port to have the the server socket listen to if we use a server socket, defaults to 0 which will randomly choose an open port |
protected java.lang.String |
_mimeType
The mime type of the content |
protected int |
_minPort
The minimum port to have the the server socket listen to if we use a server socket, defaults to 0 which will randomly choose an open port |
protected java.lang.StringBuffer |
_printBuf
A StringBuffer where verbose debugging text gets accumulated before being written to the text area. |
protected java.awt.Dialog |
_printDialog
The dialog for holding a text area where verbose debug info gets written. |
protected java.awt.TextArea |
_printText
The text area to write to in the dialog |
protected java.net.ServerSocket |
_server
The server socket we listen on if we use a server socket |
protected static boolean |
_useEcho
Should we be using echocgi.exe? |
protected static boolean |
_useSockets
Should we be using sockets? |
protected static boolean |
_verbose
Should we be writing out debugging info to a dialog we create? |
protected static java.lang.String |
BROWSER
Name of the java.lang.System property which tells us which web browser we're in. |
static int |
CANT_ACCEPT
Can't accept server connection. |
static int |
CANT_GET_OUTPUT_STREAM
Can't get output stream. |
static int |
CANT_GET_REQUEST
Can't get the full HTTP request from the client. |
static int |
CANT_OPEN_SERVERSOCKET
Can't open server socket. |
static int |
ERROR_CREATING_URL
Error while creating URL. |
static int |
ERROR_INVOKING_APP
Error while invoking application. |
static int |
ERROR_SENDING_CONTENT
Error while sending content. |
static int |
ERROR_SENDING_HEADER
Error while sending content header. |
static int |
ERROR_WRITING_FILE
Error while writing data to file. |
static java.lang.String[] |
errorMsgs
|
static int |
EXPORT_NOT_ALLOWED
Export is not allowed. |
static int |
FAILED_TO_EXPORT
Export is not allowed. |
static java.lang.String |
RB_KEY
|
protected static int |
serverTimeOut
The time out value in milli seconds set on the server socket, default value is 30000 (30 secs) |
static int |
SUCCESS
Success. |
static int |
TIMED_OUT
Timed out waiting for connection. |
Constructor Summary | |
---|---|
ContentServer2()
Constructs a ContentServer2 object. |
|
ContentServer2(java.lang.String contentType,
byte[] content,
java.lang.String fileName)
|
|
ContentServer2(java.lang.String contentType,
java.lang.StringBuffer content,
java.lang.String fileName)
|
|
ContentServer2(java.lang.String contentType,
java.lang.String content,
java.lang.String fileName)
|
Method Summary | |
---|---|
static int |
export(byte[] content,
java.lang.String contentType,
java.lang.String app,
java.lang.String args,
java.lang.String fileExt)
Exports the content given with a suggestion of an application. |
static int |
export(byte[] content,
java.lang.String contentType,
java.lang.String app,
java.lang.String args,
java.lang.String browser,
java.lang.String fileExt,
int timeOut,
int minPort,
int maxPort)
Exports the content given with suggestions for an application and a browser application (potentially null). |
static int |
export(java.lang.String content,
java.lang.String contentType,
java.lang.String app,
java.lang.String args,
java.lang.String fileExt)
Exports the content given with the suggestion of an application. |
int |
exportContentsOfOutputStream(java.io.OutputStream out,
java.lang.String app,
java.lang.String args,
java.lang.String browser)
|
java.lang.String |
getBaseTempFileName()
Returns the prefix used for creating filenames for temporary files. |
byte[] |
getContent()
Returns the content to export. |
static java.lang.String |
getEchoURL()
|
java.io.OutputStream |
getExportOutputStream(java.lang.String app,
java.lang.String args,
java.lang.String browser)
Will return an output stream that can be written to for exporting, or null if an output stream could not be returned. |
java.lang.String |
getFileExtension()
|
java.lang.String |
getFileName()
|
int |
getLocalPort()
only valid after openServer() is called |
int |
getMaxPort()
Returns the maximum port to be used when opening up a ServerSocket, the default is 0. |
java.lang.String |
getMIMEType()
Returns the MIME type we will use. |
int |
getMinPort()
Returns the minimum port to be used when opening up a ServerSocket, the default is 0. |
static java.lang.String[] |
getPossibleEchoURLs()
Returns the possible urls to look for echocgi on the originating web server. |
static int |
getTimeOut()
|
static boolean |
isExportAllowed()
Determines if exporting is allowed. |
static boolean |
isExportToDiskAllowed()
Determines if exporting to disk is allowed. |
static boolean |
isExportUsingEchoCGIAllowed()
Determines if exporting using echocgi is allowed. |
static boolean |
isExportUsingSocketsAllowed()
Determines if exporting using sockets is allowed. |
int |
openServer()
Starts the socket server. |
protected boolean |
pathExists(java.lang.String path)
Used to check whether a file exists. |
protected void |
println(java.lang.String s)
For debugging -- creates a dialog with a TextArea and sends a lot of debugging status information to it. |
protected void |
printStackTrace(java.lang.Throwable t)
For debugging -- sends the stack trace to the debug dialog. |
protected void |
readInputStream(java.net.Socket s)
Not currently used; really not useful for anything other than debugging |
static void |
resetCheck()
Before any exporting we call isExportAllowed() to check if we are allowed to export. |
void |
run()
The mini-web server thread - just serve content requested. |
protected void |
sendContent(java.io.OutputStream os)
Sends the Content to the client. |
protected java.lang.String |
sendCreateCmd()
Sends the content to the echocgi. |
protected void |
sendDeleteCmd(java.lang.String resultsFile)
Sends the delete command to echocgi to tell it to delete the content. |
protected void |
sendHeader(java.io.OutputStream os)
Sends the HTTP header to the client. |
protected void |
sendShowCmd(java.lang.String resultsFile)
Sends the show command to a new browser window. |
int |
sendToApplication(boolean deleteFile)
Writes the content to a file on the disk and opens the application pointing to that file. |
int |
sendToBrowser()
Sends the content to the browser either with a socket or with echocgi. |
int |
sendToBrowserApp()
Send to browser where browser must first be invoked. |
protected int |
sendToBrowserEcho()
Sends the content to the browser by bouncing it off the server using echocgi. |
protected int |
sendToBrowserServerSocket()
Serves the content to the browser via an internal web server. |
int |
serveContent()
Serves the content in response to a request to this mini-web server. |
protected int |
serveContentImpl()
Does the work of serving the content in reponse to a request to this mini-web server. |
boolean |
setApplication(java.lang.String application,
java.lang.String args)
Sets the application name for exporting to an application. |
void |
setBaseTempFileName(java.lang.String prefix)
Sets the prefix used for creating filenames for temporary files. |
boolean |
setBrowser(java.lang.String browser)
Sets the name of the browser. |
void |
setContent(byte[] content)
Sets the content to export. |
void |
setContent(java.lang.String content)
Sets the content to export. |
void |
setContent(java.lang.StringBuffer content)
Sets the content to export. |
void |
setContentType(java.lang.String contentType)
Deprecated. use setMIMEType |
void |
setFileExtension(java.lang.String extension)
File extension, such as csv, doc, or pdf |
void |
setFileName(java.lang.String fileName)
Sets the filename to be used with sendToApplication. |
void |
setMaxPort(int maxPort)
Sets the maximum port to be used when opening up a ServerSocket. |
void |
setMIMEType(java.lang.String mimeType)
Sets the MIME type to use when exporting. |
void |
setMinPort(int minPort)
Sets the minimum port to be used when opening up a ServerSocket. |
static void |
setPossibleEchoURLs(java.lang.String[] echoURLs)
Sets the urls to search for echocgi. |
static void |
setTimeOut(int newTimeOut)
Sets the time out value for the server socket in milli seconds. |
static void |
setVerbose(boolean verbose)
Indicates whether debug information should be sent to a debugging dialog. |
void |
showStatus(java.lang.String status)
Displays a message in the browser's status bar |
Field Detail |
---|
public static final java.lang.String RB_KEY
protected byte[] _content
protected java.lang.String _mimeType
protected java.net.ServerSocket _server
protected int _localPort
protected int _minPort
protected int _maxPort
protected static int serverTimeOut
protected java.lang.String _fileName
protected java.lang.String _extension
protected java.lang.String _browser
protected java.lang.String _application
protected static boolean _isInApplet
protected static boolean _useSockets
protected static boolean _useEcho
protected static java.lang.String _echoURL
protected static java.lang.String[] _echoURLs
protected static boolean _checked
isExportAllowed()
,
resetCheck()
protected static boolean _canWriteToDisk
protected static int _counter
protected static final java.lang.String BROWSER
java.lang.System
property which tells us which web browser we're in.
protected static boolean _verbose
protected java.lang.StringBuffer _printBuf
protected java.awt.TextArea _printText
protected java.awt.Dialog _printDialog
public static final int SUCCESS
sendToApplication(boolean)
,
sendToBrowserApp()
,
sendToBrowser()
,
sendToBrowserEcho()
,
sendToBrowserServerSocket()
,
openServer()
,
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int CANT_OPEN_SERVERSOCKET
sendToBrowserApp()
,
sendToBrowser()
,
sendToBrowserServerSocket()
,
openServer()
,
Constant Field Valuespublic static final int CANT_ACCEPT
sendToBrowser()
,
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int CANT_GET_OUTPUT_STREAM
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int TIMED_OUT
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int ERROR_SENDING_HEADER
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int ERROR_SENDING_CONTENT
sendToBrowser()
,
sendToBrowserEcho()
,
sendToBrowserServerSocket()
,
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int ERROR_CREATING_URL
sendToBrowserServerSocket()
,
sendToBrowser()
,
Constant Field Valuespublic static final int ERROR_WRITING_FILE
sendToApplication(boolean)
,
Constant Field Valuespublic static final int ERROR_INVOKING_APP
sendToApplication(boolean)
,
sendToBrowserApp()
,
Constant Field Valuespublic static final int CANT_GET_REQUEST
serveContent()
,
serveContentImpl()
,
Constant Field Valuespublic static final int EXPORT_NOT_ALLOWED
isExportAllowed()
,
Constant Field Valuespublic static final int FAILED_TO_EXPORT
isExportAllowed()
,
Constant Field Valuespublic static final java.lang.String[] errorMsgs
Constructor Detail |
---|
public ContentServer2()
public ContentServer2(java.lang.String contentType, byte[] content, java.lang.String fileName)
contentType
- The content's MIME typecontent
- The content to be sentfileName
- The file name to be used in a URL (when sendToBrowser is called)
or the file to be written to disk (when sendToApplication is called)public ContentServer2(java.lang.String contentType, java.lang.StringBuffer content, java.lang.String fileName)
contentType
- The content's MIME typecontent
- The content to be sentfileName
- The file name to be used in a URL (when sendToBrowser is called)
or the file to be written to disk (when sendToApplication is called)public ContentServer2(java.lang.String contentType, java.lang.String content, java.lang.String fileName)
contentType
- The content's MIME typecontent
- The content to be sentfileName
- The file name to be used in a URL (when sendToBrowser is called)
or the file to be written to disk (when sendToApplication is called)Method Detail |
---|
public static void resetCheck()
isExportAllowed()
public java.lang.String getBaseTempFileName()
public void setBaseTempFileName(java.lang.String prefix)
prefix
- filename prefixpublic static boolean isExportUsingSocketsAllowed()
export(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
sendToBrowser()
,
sendToBrowserServerSocket()
public static boolean isExportUsingEchoCGIAllowed()
export(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
sendToBrowser()
,
sendToBrowserEcho()
public static boolean isExportToDiskAllowed()
export(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
sendToApplication(boolean)
,
sendToBrowserApp()
public static boolean isExportAllowed()
if an applet NOT running in appletviewer:
if an application or an applet running in appletviewer:
sendToBrowser()
,
sendToBrowserServerSocket()
,
sendToBrowserEcho()
,
sendToApplication(boolean)
,
sendToBrowserApp()
,
export(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)
,
resetCheck()
public static int export(java.lang.String content, java.lang.String contentType, java.lang.String app, java.lang.String args, java.lang.String fileExt)
content
- The content to be served as a String
contentType
- The MIME type of the contentapp
- The name of the application to export toargs
- The arguments to pass to the applicationfileExt
- The extension of the file to pass it - eg csvpublic static int export(byte[] content, java.lang.String contentType, java.lang.String app, java.lang.String args, java.lang.String fileExt)
content
- The content to be served as an array of bytes.contentType
- The MIME type of the contentapp
- The name of the application to export toargs
- The arguments to pass to the applicationfileExt
- The extension of the file to pass it - eg csvpublic static int export(byte[] content, java.lang.String contentType, java.lang.String app, java.lang.String args, java.lang.String browser, java.lang.String fileExt, int timeOut, int minPort, int maxPort)
content
- The content to be served as an array of bytes.contentType
- The MIME type of the contentapp
- The name of the application to export toargs
- The arguments to pass to the applicationbrowser
- The name of the browser to use - eg "iexplore.exe"fileExt
- The extension of the file to pass it - eg csvtimeOut
- The time out value to use on the ServerSocketminPort
- The minimum port to open the ServerSocket on if a ServerSocket is used, default is 0
which causes a random port to be usedmaxPort
- The maximum port to open the ServerSocket on if a ServerSocket is used, default is 0
which causes a random port to be usedpublic java.io.OutputStream getExportOutputStream(java.lang.String app, java.lang.String args, java.lang.String browser)
app
- args
- browser
-
public int exportContentsOfOutputStream(java.io.OutputStream out, java.lang.String app, java.lang.String args, java.lang.String browser)
out
- app
- args
- browser
- public static void setTimeOut(int newTimeOut)
newTimeOut
- The time out value to usegetTimeOut()
public static int getTimeOut()
public static void setPossibleEchoURLs(java.lang.String[] echoURLs)
echoURLs
- Array of strings of relative urls to the nase URL of the originating web server.public static java.lang.String[] getPossibleEchoURLs()
public static java.lang.String getEchoURL()
public boolean setApplication(java.lang.String application, java.lang.String args)
The
- name of the application we want to export to. eg quot;C:\\Microsoft Office\\Office\\Excel.exe"args
- The arguments to pass to the applicationpublic boolean setBrowser(java.lang.String browser)
The
- name of the browser program or null for the system defined browser. Eg "c:\\Program Files\\Plus!\\Microsoft Internet\\iexplore.exe".public static void setVerbose(boolean verbose)
verbose
- true means turn debugging on, false means turn it off.public int sendToApplication(boolean deleteFile)
deleteFile
- true means delete the temporary file when finished.SUCCESS
,
ERROR_WRITING_FILE
,
ERROR_INVOKING_APP
public int sendToBrowserApp()
SUCCESS
,
ERROR_INVOKING_APP
,
CANT_OPEN_SERVERSOCKET
public int sendToBrowser()
SUCCESS
,
CANT_ACCEPT
,
ERROR_SENDING_CONTENT
,
CANT_OPEN_SERVERSOCKET
,
ERROR_CREATING_URL
protected java.lang.String sendCreateCmd() throws java.lang.Exception
java.lang.Exception
- - MalformedURLException, IOException etc.protected void sendShowCmd(java.lang.String resultsFile) throws java.lang.Exception
resultsFile
- The filename echocgi told us to ask for.
java.lang.Exception
- MalformedURLException, IOException, etc.protected void sendDeleteCmd(java.lang.String resultsFile)
resultsFile
- The file echocgi originally gave to us to identify our content.protected int sendToBrowserEcho()
ERROR_SENDING_CONTENT
,
SUCCESS
protected int sendToBrowserServerSocket()
SUCCESS
,
CANT_OPEN_SERVERSOCKET
,
ERROR_CREATING_URL
,
ERROR_SENDING_CONTENT
public void setFileName(java.lang.String fileName)
fileName
- The filename to usesendToApplication(boolean)
public java.lang.String getFileName()
public void setFileExtension(java.lang.String extension)
public java.lang.String getFileExtension()
public void setContentType(java.lang.String contentType)
public void setMIMEType(java.lang.String mimeType)
mimeType
- The MIME type to use.public java.lang.String getMIMEType()
public void setContent(byte[] content)
content
- The content.public void setContent(java.lang.StringBuffer content)
content
- The content.public void setContent(java.lang.String content)
content
- The content.public byte[] getContent()
public int getLocalPort()
public void setMinPort(int minPort)
minPort
- the new minimum port number to be usedgetMinPort()
public int getMinPort()
setMinPort(int)
public void setMaxPort(int maxPort)
maxPort
- the new maximum port number to be usedgetMaxPort()
public int getMaxPort()
setMaxPort(int)
public int openServer()
SUCCESS
,
CANT_OPEN_SERVERSOCKET
public void run()
run
in interface java.lang.Runnable
public int serveContent()
SUCCESS
,
TIMED_OUT
,
CANT_ACCEPT
,
CANT_GET_OUTPUT_STREAM
,
ERROR_SENDING_HEADER
,
ERROR_SENDING_CONTENT
,
CANT_GET_REQUEST
protected int serveContentImpl()
SUCCESS
,
TIMED_OUT
,
CANT_ACCEPT
,
CANT_GET_OUTPUT_STREAM
,
ERROR_SENDING_HEADER
,
ERROR_SENDING_CONTENT
,
CANT_GET_REQUEST
protected void sendHeader(java.io.OutputStream os) throws java.io.IOException
os
- The output stream to write the header to.
java.io.IOException
- If there was an I/O problem.protected void sendContent(java.io.OutputStream os) throws java.io.IOException
os
- The output stream to write the header to.
java.io.IOException
- If there was an I/O problem.protected boolean pathExists(java.lang.String path)
path
- The path we want to check existence on.public void showStatus(java.lang.String status)
status
- The message to display.protected void readInputStream(java.net.Socket s)
s
- The socket to read input from.protected void println(java.lang.String s)
s
- The string to write.protected void printStackTrace(java.lang.Throwable t)
t
- The error or exception to print a stack trace of.
|
Components |
|
| |||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |