Wicket: rendering HTML5 audio from plain string - java

I'd like to render HTML5 audio using Apache Wicket.
I am trying to render it from plain string. I know it's probably not the best way to do this so first I'd like to ask: is it even possible without getting errors?
Here's what I'm doing:
JAVA:
String html5AudioStr = "<audio controls><source src=\"test.mp3\" type=\"audio/mpeg\">Your browser does not support the audio element.</audio>";
add(new Label("html5Audio", html5AudioStr).setEscapeModelStrings(false));
HTML:
<span wicket:id="html5Audio"></span>
By doing this, I can see the audio player and the <audio> tag is rendered correctly. But I'm getting this error:
java.lang.ClassNotFoundException: test.mp3
And the file has this URL:
http://localhost:8080/wicket/bookmarkable/test.mp3
instead of:
http://localhost:8080/test.mp3
Is there anything I could do to fix it?

You use relative path for source. That's why browser is trying resolve absolute path relative to your page (seems it's located in /wicket/bookmarkable/).
If you are going to use / context you can simply add "/" before "test.mp3" - it should be "/test.mp3".
But if you are going to mount your application to other place rather then root context "/", I recommend to take a look to 'ContextRelativeResource'.

As of Wicket 7 media tags (audio, video, track, source) are now part of the core API.
Documentation:
Wicket User Guide 7.x - 16.3 Package resources
"Media tags - resource references with content range support"
Javadoc:
https://ci.apache.org/projects/wicket/apidocs/7.x/index.html - Packages:
org.apache.wicket.markup.html.media
org.apache.wicket.markup.html.media.audio
org.apache.wicket.markup.html.media.video
There are also some additional implementations in the "html5" module of WicketStuff about Subtitles and WebRTC.
https://github.com/wicketstuff/core/wiki/Html5

Related

DRM Framework implementation error

I am developing an ebook reader as an android app. But I want the downloaded ebook files (eg. epub) to be DRM protected. I referred to the links mentioned below and tried to implement it but resulted with an error. I am at very beginning level of implementing DRM.
My Code:
DrmManagerClient drmManagerClient = new DrmManagerClient(getApplicationContext());
DrmInfoRequest drmInfoRequest = new DrmInfoRequest(0, "text/plain"); //<--- Error in this line
DrmInfo drmInfo = drmManagerClient.acquireDrmInfo(drmInfoRequest);
Log.e("sth...: ", drmInfo.getInfoType() + "");
Error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.user.androiddrm/com.example.user.androiddrm.MainActivity}:
java.lang.IllegalArgumentException: infoType: 0,mimeType: text/plain
The basic code above is implemented based on the following documentation:
https://developer.android.com/reference/android/drm/package-summary.html
https://developer.android.com/reference/android/drm/DrmManagerClient.html#acquireDrmInfo(android.drm.DrmInfoRequest)
https://developer.android.com/reference/android/drm/DrmInfoRequest.html
https://source.android.com/devices/drm#DrmInfo
Now the Link 4 explains about drm but using MediaPlayer as example but mine is for epub files. Even though the implementation of acquireDrmInfo() is different as followed to the above 3 links.
This link specifically targets for media player Exoplayer, so not much of help.
Note: After the epub file is downloaded, the user should be able to read offline so I don't think there will be any online activity.
So how to fix above error and is there any better tutorials on using Android.Drm Framework?

Play Framework: Images not accessible in production, despite trying the "route + custom action" solution

We have a web application comprising a client in Angular2 and a server in Play Framework 2.6. This application should allow users to upload their own images. Similarly to many users here, we faced the problem of not being able to access the images in public/assets in production mode, and so we tried the solutions that have been provided to those users, with no success.
We created the following route in the conf/routes file:
GET /files/*file controllers.AdminExerciseCtrl.serve(file)
leading to the following custom action:
public Result serve(String file) {
Boolean DEVELOPMENT = Boolean.parseBoolean(ConfigFactory.load().getString("development"));
String path;
if(DEVELOPMENT) path = "public/";
else path = ConfigFactory.load().getString("root/");
return ok(new java.io.File(path + file));
}
i.e., we are saving the uploaded images to root/files/images.
We tried two way of accessing this route:
Using #routes throws an error because of the # character
Using the route in the src field (where resource.resourcePath is a variable holding the path to the image file, e.g., images/pic.jpg), the route simply isn't found (404).
<img *ngIf="resource" class="thumbnail" [src]="'/files/' + resource.resourcePath">
We are starting to despair since this project is considerably large and uploading images is a core feature. Any ideas of what we could be missing? Anything we should be doing in the client regarding the routes?
Thanks in advance.

How to delete all resources related to a specified url in zapproxy scan?

I'm using the java client api of zapproxy to detect the vulnerability of many websites automatically and dynamically.I need to release all resources(alerts,spider result,active scan result,memory usage) for the specified url and not interfere scans of other urls.
overview the whole api of zapproxy,I only got :
http://localhost:8080/UI/core/action/deleteAllAlerts
which i think will delete all alerts including those belong to the other urls.
So,how to delete resources for a specified url in zapproxy scan?
Sorry, missed this one - I've been looking for the 'zap' tag ;)
You can use the API call:
http://localhost:8080/UI/core/action/deleteSiteNode
which will delete the relevant node in the Sites tree and associated alerts.
Dont think that will delete the subtree though - could you submit a feature request for this? https://github.com/zaproxy/zaproxy/issues/new
Thanks

Vaadin 7 CustomComponent with #StyleSheet referencing image URL - Rejecting published file request for file that has not been published

I got an Rejecting published file request for file that has not been published for a custom Vaadin 7 component with an image referenced in the css:
StyleSheet("mycomponent.css")
#JavaScript( { "mycomponent1.js", "mycomponent2.js"})
public class MyComponent extends AbstractJavaScriptComponent {
//...
}
In mycomponent.css I have
background-image: url(mycomponent-bg.png);
and Vaadin tells me:
Rejecting published file request for file that has not been published: mycomponent-bg.png
Looking at the PublishedFileHandler in Vaadin 7 it says:
Serves a connector resource from the classpath if the resource has previously been registered by calling LegacyCommunicationManager#registerDependency(String, Class). Sending arbitrary files from the classpath is prevented by only accepting resource names that have explicitly been registered. Resources can currently only be registered by including a {#link JavaScript} or {#link StyleSheet} annotation on a Connector class.
Doing a manual registration in the Component doesn't work:
LegacyCommunicationManager lcm = new LegacyCommunicationManager(this.getSession());
lcm.registerDependency("mycomponent-bg.png", this.getClass()); // 1.try
lcm.registerDependency("mypath/mycomponent-bg.png", this.getClass()); // 2.try
Question: What is the right way to have css styled custom components with images in Vaadin 7? OR: How can I add a arbitrary file to the dependencies?
A solution to this problem would be, to add static image resources to a subfolder of /VAADIN/ as all resources contained within this folder can always be accessed directly and in a static manner or even move the stylesheet and all images used by it to a Vaadin theme (for details on how to do this, please refer to the Themes chapter in the Book of Vaadin).
An alternative (although a rather messy one) would also be to make the required image files a dependency of your component by including them in the #JavaScript annotation, which will lead to them being correctly published along with your component and its stylesheets, but will also produce an error in your browser when viewing the page.
Regarding this topic, there is also a (now closed) ticket and a forum thread which further details the solutions mentioned above.
for Vaadin 7 ... If your external javascript or css loads images then it should be placed in /VAADIN/ folder . for example if we have a file named common.js inside /VAADIN/js directory , you can refer the same in java UI or AbstractJavaScriptComponent file using the path #JavaScript({"vaadin://js/common.js"}) and put all the image files relative to that directory ... it will work fine ....
#StyleSheet(..your css..)
public class YourAddonClass ... {
static {
VaadinSession.getCurrent().getCommunicationManager().
registerDependency("mycomponent-bg.png",
ClassInSamePackageAsmycomponent-bg.class);
}
}

Load Custom Fonts in JavaFX + Clojure

As background, I realize there are about 5 other posts on Stack Overflow about this, bur I've looked at the responses and researched this for countless hours with no real solution. Otherwise I wouldn't have posted here.
I'm new to JavaFX, and I like Clojure, so I'm using chrisx's clj-javafx project from Github as a Clojure wrapper for JavaFX. Most operations work great, such as placing components on the scene/stage and styling them with CSS. There's just one issue: I want to import a custom font, and so far I haven't been able to.
I've tried multiple methods. The Java version of the first thing I tried is:
Font.loadFont(<myClass>.class.getResource("mpsesb.ttf").toExternalForm(), 14);
I tried to do that in Clojure using Java interop but I'm not sure how Clojure handles Java classes. Instead I used clojure.java.io.resource:
(use '[clojure.java.io :only [resource]])
(resource "mpsesb.tff")
And it prints the URL of the .ttf file just fine.
Then I tried to use that URL and use it as a parameter in the Font.loadFont method, which didn't work. After playing around with seriously 100 permutations of dot operators and Java methods and classes, I got the following to not print any errors, unlike the other combinations I tried:
(Font/loadFont "fonts/ttf/mpsesb.ttf")
But it just returns nil, which according to the JavaFX API means the font didn't actually load. I even tried fake pathnames and they also returned nil without errors, so the Font/loadFont function seems less promising than I thought.
I finally tried using CSS to load the font instead of using Java to import it:
#font-face {
font-family: 'MyrProSemiExtBold';
src: url('file:/Users/<restOfPath>/mpsesb.ttf');
}
.label {
-fx-font-size: 14px;
-fx-font-weight: normal;
-fx-font-family: 'MyrProSemiExtBold';
}
But no luck. The labels just show up as the default font (Lucida Grande) at 14 pt.
Thank you ahead of time for any suggestions you may have.
EDIT:
Thanks, #jewelsea. The first thing I did after reading what you wrote is get Java 8 + JDK 8 (build b101). I'm using Sublime Text, so I got SublimeREPL to recognize JavaFX using Maven like so in Terminal:
$ mvn deploy:deploy-file -DgroupId=local.oracle -DartifactId=javafxrt -Dversion=8.0 -Dpackaging=jar -Dfile=/System/Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/jre/l‌​ib/ext/jfxrt.jar -Durl=file:/Users/vaniificat/.m2/repository
Most of the JavaFX-referent code worked, but now the whole javafx.scene.control package doesn't work, which means no Labels, no Buttons, no TextFields, etc.... :
> (import javafx.scene.control.Button)
: NoClassDefFoundError Could not initialize class javafx.scene.control.Button
java.lang.Class.forName0 (Class.java:-2)
> (import javafx.scene.control.Label)
: NoClassDefFoundError javafx.scene.control.Labeled
java.lang.Class.forName0 (Class.java:-2)
> (import javafx.scene.control.TextField)
: NoClassDefFoundError javafx.scene.control.Control
java.lang.Class.forName0 (Class.java:-2)
> (import '(javafx.scene.control Button Label PasswordField TextField))
: NoClassDefFoundError Could not initialize class javafx.scene.control.Button
java.lang.Class.forName0 (Class.java:-2)
A possible reference to these errors may be found at https://forums.oracle.com/thread/2526150. Basically the gist is that JavaFX 8 is only in beta so we can't expect it to work 100%. Hmm!
I opened an issue on JIRA: https://bugs.openjdk.java.net/browse/JDK-8093894. Once this gets addressed I can more fully explore #jewelsea 's other suggestions.
These are mostly suggestions rather than definitive solutions
On #font-face
Use Java 8 if you can. It has support for the #font-face css notation (which earlier versions of JavaFX such as 2.2 do not). It also has a revamped font system which might be a bit more robust. Because the font face can be loaded via css in Java 8, you may not need to solve your loading resources relative to the class location issue.
On Font.loadFont
You should be able to use the Font.loadFont method as you are trying to use it. JavaFX 2.x has supported custom fonts from it's initial release. There are some steps for loading fonts in JavaFX in plain Java: How to embed .ttf fonts is JavaFx 2.2?. I know you have already looked at that and it hasn't helped - just including it here for reference so that somebody who really knows clojure but not JavaFX may help to troublehoot the issue.
Check that your target font is compatible
Double-check that the particular font that you are using loads and is used in a regular Java based JavaFX application (just to ensure that there is not something incompatible between the font file and the JavaFX system on your platform).
You may need further help
I don't know how loading relative resources works in clojure either (or maybe there is some other issue with your code) - but perhaps a clojure expert can post another answer that allows it to work. You may want to edit your question to include an sscce.
Update: As was pointed out, the original answer did not directly answer the OP's question. I've created a blog post about the original technique in case anyone is interested.
Here is a short program that does what you want.
(ns demo.core
(:gen-class
:extends javafx.application.Application)
(:import
[javafx.application Application]
[javafx.event EventHandler]
[javafx.scene Scene]
[javafx.scene.control Button]
[javafx.scene.layout StackPane]
[javafx.scene.text Font])
(:require [clojure.java.io :as jio]))
(defn- get-font-from-resource
"Load the named font from a resource."
[font-name]
(let [prefix "demo/resources/"
url (jio/resource (str prefix font-name))
fnt (Font/loadFont (.toExternalForm url) 20.0)]
fnt))
(defn -start
"Build the application interface and start it up."
[this stage]
(let [root (StackPane.)
scene (Scene. root 600 400)
fnt (get-font-from-resource "ITCBLKAD.TTF")
btn (Button. "Press Me!")]
(.setOnAction btn
(reify EventHandler
(handle [this event]
(doto btn
(.setText (str "This is " (.getName fnt)))
(.setFont fnt)))))
(.add (.getChildren root) btn)
(doto stage
(.setTitle "Font Loading Demo")
(.setScene scene)
(.show))))
(defn -main
[& args]
(Application/launch demo.core args))
In this project, I placed the font file in resources, a sub-directory of demo - where the Clojure source is stored - hence the "prefix" in the function get-font-from-resource.
It looks like the problem you might have been having with loadFont was in your conversion from the URL to the String form. The external form is an absolute path starting at the root directory on the drive.
Tip: You probably know this, but one thing that continually screws me up are methods in JavaFX that require double parameters, like Font/loadFont. I'm used to Java just promoting integer arguments to double. In Clojure, if you use an integer where a double is required, the program fails with a less than useful error message.

Categories