public class LocalSingleHttpServer extends Object
HttpServer for a local player.
The primary goal of this implementation is to serve protected contents to a local client,
typically a VideoView.
The main characteristics of the server, security oriented, are:
Permissions
This class requires only the permissions for HttpServer.
Example
Here is a partial sample of usage in an application:
private void myPlay(String path) {
mServer = new LocalSingleHttpServer();
mServer.setCipher(myGetCipher())
.start();
path = mServer.getURL(path);
mVideoView.setVideoPath(path);
mVideoView.start();
}
public void onCompletion(MediaPlayer mp) { // MediaPlayer.OnCompletionListener interface
mServer.stop();
}
This is only a very simplified piece of code, there is more to do:
manage the exceptions, stop the server in every circumstance and yet only if running.HttpServer.JsInterface| Constructor and Description |
|---|
LocalSingleHttpServer()
Default constructor.
|
| Modifier and Type | Method and Description |
|---|---|
HttpServer.JsInterface |
getJsInterfaceObject()
Returns a Java object for injection into a WebView.
|
String |
getURL(DocumentFile docFile)
Returns the URL to serve a resource managed by a document provider.
|
String |
getURL(int mainVersion,
int patchVersion,
String entryPath)
Returns the URL to serve a resource embedded into APK Expansion Zip files.
|
String |
getURL(String path)
Returns the URL to serve a resource specified by the provided path.
|
String |
getURL(String zipPath,
String entryPath)
Returns the URL to serve a resource embedded into a Zip file.
|
HttpServer |
setCipher(Cipher cipher)
Sets the Cipher for decryption.
|
HttpServer |
setCipherFactory(CipherFactory cipherFactory)
Sets a cipher factory for decryption.
|
HttpServer |
setDataSource(DataSource dataSource)
Sets a custom data provider.
|
void |
start()
Starts the server.
|
void |
stop()
Stops the server.
|
public LocalSingleHttpServer()
throws IOException
IOException - If an error occurs while creating the server socket.public String getURL(String path)
HttpServerFor example: http://127.0.0.1:54123/path/to/a/file.ext
The path parameter supports various meanings:
getURL in interface HttpServerpath - The path of a resource.null if the server is not initialized or a necessary library is missing.public String getURL(DocumentFile docFile)
HttpServergetURL in interface HttpServerdocFile - The document to target.null if the server is not initialized.public String getURL(String zipPath, String entryPath)
HttpServergetURL in interface HttpServerzipPath - The path of the Zip file.entryPath - The path to the desired file, relative to the root of the ZIP file contents.null if the server is not initialized or the zipfile library is missing.public String getURL(int mainVersion, int patchVersion, String entryPath)
HttpServergetURL in interface HttpServermainVersion - The parameter defined for getAPKExpansionZipFile() in the APK Expansion Zip Library.patchVersion - The parameter defined for getAPKExpansionZipFile() in the APK Expansion Zip Library.entryPath - The path to the desired file, relative to the root of the ZIP file contents.null if the server is not initialized or the zipfile library is missing.public HttpServer.JsInterface getJsInterfaceObject()
HttpServerExample: webview.addJavascriptInterface(mServer.getJsInterfaceObject(), "serverObject");
getJsInterfaceObject in interface HttpServerpublic HttpServer setDataSource(DataSource dataSource)
HttpServerThis is an option, as there is already a default data provider instance, able to deal with files of various kind (regular, zip, expansion, asset, SMB, remote, ...). Note that this default instance is lost once you give your own.
setDataSource in interface HttpServerdataSource - A data provider. Cannot be null.public HttpServer setCipher(Cipher cipher)
HttpServerThis is an option, by default there is no cryptographic processing.
The object is immediately forwarded to the current data provider,
so if you intend to use a custom data provider,
you must set it before with setDataSource().
Alternatively, you may use HttpServer.setCipherFactory(CipherFactory).
In case you use both, this method has precedence over CipherFactory.getCipher().
But this is not a recommended design: a better approach is to group all cipher related actions
at the same place.
setCipher in interface HttpServercipher - The cipher for decrypting the resource,
or null to deactivate the cryptographic processing.public HttpServer setCipherFactory(CipherFactory cipherFactory)
HttpServerThis is an option, by default there is no cryptographic processing.
The object is immediately forwarded to the current data provider,
so if you intend to use a custom data provider,
you must set it before with setDataSource().
Alternatively, you may use HttpServer.setCipher(Cipher).
setCipherFactory in interface HttpServercipherFactory - The instance to generate ciphers on demand,
or null to deactivate the cryptographic processing.public void start()
HttpServerOnce started, use HttpServer.stop() to stop a running server.
A stopped server cannot be started again, instead create another one.
start in interface HttpServerpublic void stop()
HttpServerStopping a server that is not started is illegal but has no penalty.
The server should be either waiting for the next request from its connected client, either waiting for a client to connect. Anyway, and even if there is a transmission in progress, all sockets are immediately closed.
stop in interface HttpServer