Display an Image attachment in XPages via a Java Bean - xpages

I have a public facing application that is architected with multiple nsfs. I have one nsf that's public facing that manages data through Java controllers in the non-public facing nsfs. For security reasons the non-public nsfs do not allow URL browsing so I can't use the usual "/0/" + UNID + "/$FILE/" + PhotoFilename to display the image on the XPage.
In the controller I can get the attachment (EmbeddedObject) from the document, but I'm not sure how to get it to display in an <xp:image> or <xp:inputRichText> control.
I am using the OpenNTF api, but I can't see anything there that seems to help.
Thanks,
Scott.

One solution: base64 encode the image and use the encoded string in an img tag. Here's a simple code example that uses the inputstream from the EmbeddedObject in a method in a Java controller:
public String getImageAsBase64(InputStream inputStream) {
return Base64.encodeBytes(IOUtils.toByteArray(inputStream));
}
You can then display the base64 encoded image (assuming your Java controller is called 'controller'):
<img src="data:image/jpeg;base64,#{javascript:controller.getImageAsBase64()}" />

Related

I want to create an angular app to show live code preview like Jsbin

I'm creating the Angular app which lets user save his html code (maybe it has style and script tag to control the view) and there is a live preview next to it. User can save the code and other users can come to see the code and the preview. I also worry about the security because of script tag and I want the script to work only with the code that user provides (Not allow to control or get the data in the parent frame). So I need some suggesting of how to do it.
I have tried the preview with iFrame by giving the value through the 'srcdoc' property, but it looks like the security is bad.
You would not need to use an iframe in that situation, you can just render an HTML string inside of a div element using the innerHtml input like so:
<div [innerHTML]="htmlString"></div>
Where htmlString is a string containing the HTML code. You will have to sanitize the content of that variable with the DomSanitizer.
constructor(private domSanitizer: DomSanitizer){}
...
ngOnInit() {
this.htmlString = this.domSanitizer.bypassSecurityTrustHtml(yourHTMLString);
}

Displaying image string data in picture box.

I’ve to display a image in a picture box in VisualWebGui. I’ve image in string format.
string ImageString_P;
FileStream fs_P = new FileStream(LocalDirectory + "Page_2.tif", FileMode.Open, FileAccess.Read);
byte[] picbyte_P = new byte[fs_P.Length];
fs_P.Read(picbyte_P, 0, Convert.ToInt32(fs_P.Length));
ImageString_P = Convert.ToBase64String(picbyte_P);
Now, how can I display this image(ImageString_P) in the picture box. Should I create the image of this string data or can I directly display this data in the PictureBox?
If I’ll create the image in a path(suppose “c:\xyz.jpg”) . How it (xyz.jpg) will be displayed in the picturebox.
Visual WebGui is a web application and as such it basically needs to let the browser specifically request any graphics data that should be rendered, which is fundamentally different from that of desktop applications, where you can simply assign the graphics data itself to the image property of a PictureBox.
If you study how a webpage with a PictureBox is rendered to the browser in Visual WebGui, you will see that the PictureBox is rendered as an img tag with the source being set to an Url, which is responsible for serving the image to the browser. When the browser sees that Url on the img tag, it issues another request to the server for the contents of that Url. This "secondary" request is called a gateway request in Visual WebGui.
To serve the graphics to the browser, you need some kind of Gateway in your Visual WebGui application. There are a few types of predefined gateways in Visual WebGui, like for Images (ImageResourceHandle) and icons (IconResourceHandle), but in this case you have a dynamically generated image, so you will need to define your own gateway to serve the graphics contents.... or you can write the image data to, say, Resources\Images folder of your application and then use an ImageResourceHandle to reference it.
Defining your own gateways in Visual WebGui is very simple, and you can see quite a few examples here.
Hope this helps,
Palli
You can do like this:
string ImageString_P;
FileStream fs_P = new FileStream(LocalDirectory + "Page_2.tif", FileMode.Open, FileAccess.Read);
byte[] picbyte_P = new byte[fs_P.Length];
this.picMyPicture.Image = new DynamicStreamResourceHandle(contentBitmap, "image/jpeg");
and it should render fine.

How to obtain the download url for a document library content programmatically

I have several contents in my document library (images, documents, videos...) and I want to obtain the download url for each of them programmatically, so I can show them on a jsp in a custom portlet.
Investigating, I have found this page with a possible solution:
https://www.liferay.com/community/forums/-/message_boards/message/11073293
but I'm not comfortable with it, since it implies building the url manually with several parameters.
I would like to know if there is a better way ("Liferay way") to do this.
I am afraid but that is the liferay way :-).
If you still need more confirmation check out source code of the DLUtil#getPreviewURL method.
This method has been used by liferay's Documents & Media portlet and Document and Media display portlet. Also you can check-out the JSP source as to how these portlets use the above method /portal-web/docroot/html/portlet/document_library/action/download.jspf .
P.S.:
You can convert DLFileEntry to FileEntry with this static method.
By such questions I see always to Liferay sources. Here is the fragment that build the file download url in "Documents and Media Library" portlet:
<liferay-ui:input-resource
url='<%= themeDisplay.getPortalURL() + themeDisplay.getPathContext() + "/documents/" + themeDisplay.getScopeGroupId() + StringPool.SLASH + fileEntry.getUuid() %>'
/>
but you are right, i would prefer a methode like DLFile.getUrl() too.

j2me Lwuit form images from RSS feeds are not being loaded correctly

I have a LWUIT form in my j2me based application. I am getting my data through RSS Feeds(including image path). Now i want to show the images through RSS feeds image path in my LWUIT Form. I am using HTMLComponent for that purpose. The html based text is loading fine but the images are not being shown. Following is my code snippet.
HTMLComponent htmlComp = new HTMLComponent();
htmlComp.setPreferredSize(new Dimension(300,300));
String imagePath=myItem.getImagePath().trim();
htmlComp.setShowImages(true);
String myHtml="<b>Hello</b> <br/> <img src= \""+imagePath+"\" /> ";
System.out.println(myHtml);
htmlComp.setHTML(myHtml,null,null,false);
addComponent(htmlComp);
The image path is like "http://toffeetv.com/wp-content/uploads/2012/06/f_swan-150x150.jpg".
Why the images are not being shown? Can anybody help me with an appropriate code example?
The HTML Component accepts a base URL as one of its arguments to that method. Just set the URL to the appropriate path and it should work as expected.

Use the value entered in a JSF page for Java Applet

I have created a JSF page which asks the user to enter a value, and this value is being processed via Applet and create a barcode image based on the inputted value. My problem is how can i get the value from the JSF Page and use it in my Applet.. Thanks!
Use JavaScript. E.g.
<h:inputText ... onchange="updateBarcodeApplet(this.value)" />
...
<applet id="barcodeApplet" ...></applet>
with this JS
<script>
function updateBarcodeApplet(value) {
var barcodeApplet = document.getElementById("barcodeApplet");
barcodeApplet.updateValue(value);
}
</script>
and in Applet
public void updateValue(String value) {
// Do your business here.
}
(yes, all Applet's public methods are just available as is in JS)
Needless to say, using an applet for this job is pretty clumsy. Not all clients support or even appreciate applets. I'd also opt for a simple <img> element with a servlet which returns the image as suggested by Denisk. You just have to update the <img src> by JSF or JavaScript.
Why do you do it in a hard way? You don't really want an applet here, create the image on server, serialize it to response stream and display it as plain image.

Resources