public class UtilServer extends Object
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).
| Modifier and Type | Field and Description |
|---|---|
static String |
PORT_PROP
System Property key giving a preferred port number for the server.
|
| Constructor and Description |
|---|
UtilServer(HttpServer server)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
copy(InputStream in,
OutputStream out)
Copies the content of an input stream to an output stream.
|
URL |
exportFile(File file)
Exposes a file in the local filesystem as a publicly visible URL.
|
URL |
exportResource(String resource)
Deprecated.
since after 1.3.5,
use instead
exportResource(java.net.URL) |
URL |
exportResource(URL localUrl)
Exposes a resource from a given URL as a publicly visible URL.
|
String |
getBasePath(String txt)
May be used to return a unique base path for use with this class's
HttpServer.
|
static UtilServer |
getInstance()
Returns the default instance of this class.
|
MultiURLMapperHandler |
getMapperHandler()
Returns a handler for mapping local to external URLs associated with
this server.
|
ResourceHandler |
getResourceHandler()
Returns a handler for general purpose resource serving associated with
this server.
|
HttpServer |
getServer()
Returns the HttpServer associated with this object.
|
static void |
setInstance(UtilServer server)
Sets the default instance of this class.
|
public static final String PORT_PROP
public UtilServer(HttpServer server) throws IOException
getInstance() method.server - HTTP server providing base servicesIOExceptionpublic HttpServer getServer()
public MultiURLMapperHandler getMapperHandler()
public ResourceHandler getResourceHandler()
public URL exportResource(URL localUrl) throws IOException
localUrl is
only visible locally, for instance on this JVM's classpath.localUrl - URL visible at least within this JVMIOExceptionpublic URL exportResource(String resource) throws IOException
exportResource(java.net.URL)resource - fully qualified path to a resource in the current
classpath; separators are "/" charactersIOExceptionpublic URL exportFile(File file) throws IOException
file - a file on a filesystem visible from the local hostIOExceptionpublic String getBasePath(String txt)
txt - basic text for base pathtxt, but may be adjusted to ensure uniquenesspublic static UtilServer getInstance() throws IOException
getInstance() is called.IOExceptionpublic static void setInstance(UtilServer server)
server - default instance to be returned by getInstance()public static void copy(InputStream in, OutputStream out) throws IOException
in - input streamout - output streamIOExceptionCopyright © 2008–2024. All rights reserved.