Package org.astrogrid.samp.httpd
Class UtilServer
java.lang.Object
org.astrogrid.samp.httpd.UtilServer
Utility class for use with HttpServer.
This class performs two functions. Firstly it provides a static
getInstance()
method which allows its use in a singleton-like way.
The constructor is public, so singleton use is not enforced, but if
you need a server but don't need exclusive control over it, obtaining
one in this way will ensure that you don't start a new server
(which requires a new socket and other resources) if a suitable one
is already available.
Secondly, it provides some utility methods,
exportResource(java.net.URL)
and exportFile(java.io.File)
,
useful for turning files or classpath resources into
publicly viewable URLs, which is sometimes useful within a SAMP
context (for instance when providing an Icon URL in metadata).
- Since:
- 22 Jul 2009
- Author:
- Mark Taylor
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic void
copy
(InputStream in, OutputStream out) Copies the content of an input stream to an output stream.exportFile
(File file) Exposes a file in the local filesystem as a publicly visible URL.exportResource
(String resource) Deprecated.exportResource
(URL localUrl) Exposes a resource from a given URL as a publicly visible URL.getBasePath
(String txt) May be used to return a unique base path for use with this class's HttpServer.static UtilServer
Returns the default instance of this class.Returns a handler for mapping local to external URLs associated with this server.Returns a handler for general purpose resource serving associated with this server.Returns the HttpServer associated with this object.static void
setInstance
(UtilServer server) Sets the default instance of this class.
-
Field Details
-
PORT_PROP
System Property key giving a preferred port number for the server. If unset, or 0, or the chosen port is occupied, a system-chosen value will be used. The property name is "jsamp.server.port".- See Also:
-
-
Constructor Details
-
UtilServer
Constructor. Note, it may be more appropriate to use thegetInstance()
method.- Parameters:
server
- HTTP server providing base services- Throws:
IOException
-
-
Method Details
-
getServer
Returns the HttpServer associated with this object.- Returns:
- a running server instance
-
getMapperHandler
Returns a handler for mapping local to external URLs associated with this server.- Returns:
- url mapping handler
-
getResourceHandler
Returns a handler for general purpose resource serving associated with this server.- Returns:
- resource serving handler
-
exportResource
Exposes a resource from a given URL as a publicly visible URL. This is typically used if the givenlocalUrl
is only visible locally, for instance on this JVM's classpath.- Parameters:
localUrl
- URL visible at least within this JVM- Returns:
- URL for external reference to the resource
- Throws:
IOException
-
exportResource
Deprecated.since after 1.3.5, use insteadexportResource(java.net.URL)
Exposes a resource from the JVM's classpath as a publicly visible URL. The classloader of this class is used, which may not be the right one for the resource in question. For that reason, this utility method is deprecated in favour of the overloaded method that takes a URL.- Parameters:
resource
- fully qualified path to a resource in the current classpath; separators are "/" characters- Returns:
- URL for external reference to the resource
- Throws:
IOException
-
exportFile
Exposes a file in the local filesystem as a publicly visible URL.- Parameters:
file
- a file on a filesystem visible from the local host- Returns:
- URL for external reference to the resource
- Throws:
IOException
-
getBasePath
May be used to return a unique base path for use with this class's HttpServer. If all users of this server use this method to get base paths for use with different handlers, nameclash avoidance is guaranteed.- Parameters:
txt
- basic text for base path- Returns:
- base path; will likely bear some resemblance to
txt
, but may be adjusted to ensure uniqueness
-
getInstance
Returns the default instance of this class. The first time this method is called a new daemon UtilServer is (lazily) created, and started. Any subsequent calls will return the same object, unlessgetInstance()
is called.- Returns:
- default instance of this class
- Throws:
IOException
-
setInstance
Sets the default instance of this class.- Parameters:
server
- default instance to be returned bygetInstance()
-
copy
Copies the content of an input stream to an output stream. The input stream is always closed on exit; the output stream is not.- Parameters:
in
- input streamout
- output stream- Throws:
IOException
-
exportResource(java.net.URL)