Package org.astrogrid.samp.httpd

# Class UtilServer

java.lang.Object
org.astrogrid.samp.httpd.UtilServer

public class UtilServer extends Object
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
Modifier and Type
Field
Description
`static final String`
`PORT_PROP`
System Property key giving a preferred port number for the server.

## Constructor Summary

Constructors
Constructor
Description
`UtilServer(HttpServer server)`
Constructor.

## Method Summary

Modifier and Type
Method
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.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### PORT_PROP

public static final String 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:
  - Constant Field Values

## Constructor Details

### UtilServer

public UtilServer(HttpServer server) throws IOException
Constructor. Note, it may be more appropriate to use the `getInstance()` method.

Parameters:
  `server` - HTTP server providing base services
Throws:
  `IOException`

## Method Details

### getServer

public HttpServer getServer()
Returns the HttpServer associated with this object.

Returns:
  a running server instance

### getMapperHandler

public MultiURLMapperHandler getMapperHandler()
Returns a handler for mapping local to external URLs associated with this server.

Returns:
  url mapping handler

### getResourceHandler

public ResourceHandler getResourceHandler()
Returns a handler for general purpose resource serving associated with this server.

Returns:
  resource serving handler

### exportResource

public URL exportResource(URL localUrl) throws IOException
Exposes a resource from a given URL as a publicly visible URL. This is typically used if the given `localUrl` 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

public URL exportResource(String resource) throws IOException
Deprecated.
since after 1.3.5, use instead `exportResource(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

public URL exportFile(File file) throws IOException
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

public String getBasePath(String txt)
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

public static UtilServer getInstance() throws IOException
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, unless `getInstance()` is called.

Returns:
  default instance of this class
Throws:
  `IOException`

### setInstance

public static void setInstance(UtilServer server)
Sets the default instance of this class.

Parameters:
  `server` - default instance to be returned by `getInstance()`

### copy

public static void copy(InputStream in, OutputStream out) throws IOException
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 stream
  `out` - output stream
Throws:
  `IOException`