public class LocalSingleHttpServer extends Object
The primary goal of this implementation is to serve protected contents to a VideoView
in a streaming mode.
It makes sense when the source media file is encrypted and a storage in decrypted form is not admitted,
even temporary for the playing duration.
If there is no cryptographic nor any other content processing, then giving directly
the file path to the player is just enough.
The main characteristics of the server, security oriented, are:
The encrypted resource can be sourced locally from:
Environment.getExternalStorageDirectory()
.zip
file.obb
APK Expansion Zip fileOr remotely from:
Permissions
This class requires the INTERNET
permission.
Example
Here is a partial sample of usage in an application:
private void myPlay(String path) { mServer = new LocalSingleHttpServer(); mServer.setCipher(myGetCipher()); mServer.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.
Modifier and Type | Class and Description |
---|---|
static interface |
LocalSingleHttpServer.CipherFactory
A cipher factory.
|
static interface |
LocalSingleHttpServer.DataSource
A data provider.
|
static class |
LocalSingleHttpServer.Diagnostic
Represents a potential reason of dysfunction.
|
static class |
LocalSingleHttpServer.FileDataSource
The default implementation for a data provider that obtains its content from a file,
or more generally from a resource.
|
Constructor and Description |
---|
LocalSingleHttpServer()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
static List<LocalSingleHttpServer.Diagnostic> |
diagnose()
Returns a list of potential reasons of dysfunction.
|
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.
|
void |
setCipher(Cipher cipher)
Sets the Cipher for decryption.
|
void |
setCipherFactory(LocalSingleHttpServer.CipherFactory cipherFactory)
Sets a cipher factory for decryption.
|
void |
setDataSource(LocalSingleHttpServer.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 static List<LocalSingleHttpServer.Diagnostic> diagnose()
Note that it is automatically called when running in developer mode
,
in which case diagnostics are output in the log as warning lines.
public String getURL(String path)
For example: http://127.0.0.1:54123/path/to/a/file.ext
The path parameter supports various meanings:
path
- The path of a resource.null
if the server is not initialized or a necessary library is missing.public String getURL(String zipPath, String entryPath)
zipPath
- 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)
mainVersion
- 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 void setDataSource(LocalSingleHttpServer.DataSource dataSource)
This 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.
dataSource
- A data provider. Can not be null
.public void setCipher(Cipher cipher)
This 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 setCipherFactory(CipherFactory)
.
In case you use both, this method has precedence over LocalSingleHttpServer.CipherFactory.getCipher()
.
But this is not a recommended design: a better approach is to group all cipher related actions
at the same place.
cipher
- The cipher for decrypting the resource,
or null
to deactivate the cryptographic processing.public void setCipherFactory(LocalSingleHttpServer.CipherFactory cipherFactory)
This 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 setCipher(Cipher)
.
cipherFactory
- The instance to generate ciphers on demand,
or null
to deactivate the cryptographic processing.public void start()
Once started, use stop()
to stop a running server.
A stopped server can not be started again, instead create another one.
public void stop()
Stopping 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.