I am using vaadin video to integrate some videos in my application. These videos are on web which are password protected. I am using following approach but its not working for me. It keeps asking me for username and password.
URL url = new URL(Constants.VIDEO_HTTP_ADDRESS + fileName + ".mp4");
URLConnection uc = url.openConnection();
String userpass = Constants.VIDEO_HTTP_ADDRESS_USERNAME + ":" + Constants.VIDEO_HTTP_ADDRESS_PASSWORD;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
....
....
final Video v = new Video(label);
ExternalResource fileResource;
fileResource = new ExternalResource(url);
v.setSources(fileResource);
I have user name and password, I want to integrate these videos in my application, so that they dont need the username and password explicitly.
As pointed by @f1sh in his comment, you need to use your URLConnection
to get the video resource. Instead of ExternalResource
you can use StreamResource
which allows to load the resource from provided InputStream
.
Something like that should work:
Video v = new Video();
v.setSource(new StreamResource(new StreamSource() {
@Override
public InputStream getStream() {
try {
URL url = new URL(Constants.VIDEO_HTTP_ADDRESS + fileName + ".mp4");
URLConnection uc = url.openConnection();
String userpass = Constants.VIDEO_HTTP_ADDRESS_USERNAME + ":" + Constants.VIDEO_HTTP_ADDRESS_PASSWORD;
String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
uc.setRequestProperty("Authorization", basicAuth);
return uc.getInputStream();
} catch (IOException e) {
//add some exception handling here
}
}
}, fileName + ".mp4"));