public class WifiSingleHttpServer extends Object
HttpServer
for a remote player.
The primary goal of this implementation is to serve protected contents to a remote client, typically a Chromecast device.
The main characteristics of the server, security oriented, are:
Permissions
In addition to the permissions for HttpServer
, this class requires the
ACCESS_WIFI_STATE
permission.
Example
Here is a partial sample of usage in an application:
private void myPlay(CastDevice cd, String path) { mServer = new WifiSingleHttpServer(); mServer.setAllowedClient(cd) .setCipher(myGetCipher()) .start(); path = mServer.getURL(path); // MediaInfo mediaInfo = new MediaInfo.Builder(path).build(); // mMediaPlayer.load(mApiClient, mediaInfo, true); }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 |
---|
WifiSingleHttpServer()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
HttpServer.JsInterface |
getJsInterfaceObject()
Returns a Java object for injection into a WebView.
|
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.
|
WifiSingleHttpServer |
setAllowedClient(com.google.android.gms.cast.CastDevice cd)
Allows a targeted client to hit the server.
|
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 WifiSingleHttpServer() throws IOException
IOException
- If an error occurs while creating the server socket.public WifiSingleHttpServer setAllowedClient(com.google.android.gms.cast.CastDevice cd)
cd
- The Chromecast device to accept connections from.public String getURL(String path)
HttpServer
For example: http://127.0.0.1:54123/path/to/a/file.ext
The path parameter supports various meanings:
getURL
in interface HttpServer
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)
HttpServer
getURL
in interface HttpServer
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)
HttpServer
getURL
in interface HttpServer
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 HttpServer.JsInterface getJsInterfaceObject()
HttpServer
Example: webview.addJavascriptInterface(mServer.getJsInterfaceObject(), "serverObject");
getJsInterfaceObject
in interface HttpServer
public HttpServer setDataSource(DataSource dataSource)
HttpServer
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.
setDataSource
in interface HttpServer
dataSource
- A data provider. Cannot be null
.public HttpServer setCipher(Cipher cipher)
HttpServer
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 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 HttpServer
cipher
- The cipher for decrypting the resource,
or null
to deactivate the cryptographic processing.public HttpServer setCipherFactory(CipherFactory cipherFactory)
HttpServer
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 HttpServer.setCipher(Cipher)
.
setCipherFactory
in interface HttpServer
cipherFactory
- The instance to generate ciphers on demand,
or null
to deactivate the cryptographic processing.public void start()
HttpServer
Once started, use HttpServer.stop()
to stop a running server.
A stopped server cannot be started again, instead create another one.
start
in interface HttpServer
public void stop()
HttpServer
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.
stop
in interface HttpServer