Get nsf resource from a managed bean - xpages

I'm missing something. In Eclipse I drafted a class to make PDF and imported a couple of fonts into a the package and accessed them like this:
Font garamondBlue = FontFactory.getFont("/fonts/Garamond.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
I'm moving this to an XPage bean now, and I can't find how to get them the same way. I could put them in as file resources, but how do I access them there?
I added images to classic Java agents back in the pre-XPage days, can we do something like that with a bean?
Thanks,
Brian

You can read file resources as input streams like this:
InputStream fileResource = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("filename");

Related

Adding UIWebView to second (not first) Storyboard view controller

Getting started with xCode 4.6.3 and I'm running into a hiccup.
I've built a simple app using the UIWebView to display a local HTML file and it's contents. I used that to debug any issues with the HTML and/or image displays and I'm all set.
Now I need to recreate that within a larger project I've built with storyboards for all my navigations between multiple view controllers. My issue comes when I'm trying to control-click drag from the WebView into the ViewController.h code below the #interface like many tutorials show, and that worked fine within my smaller single view controller app. It won't do it. I know I'm missing something obvious here. Am I going to have the set up these screens (I have multiple ones to do this same way) as separate xib files and add them into my main project?
Thanks for any help and clarification.
-Chris
You can create a class called for example myWebViewController and in Interface builder add a UIWebView to it. The control+drag to the header file will work.
Every time you want a UIViewController that has a browser in it, define its class as myWebViewController in Interface Builder.
Try not to repeat code. If you see UIViewControllers or any other UIView...that do the same thing, you can group them into a class that you use over and over.

developing library controls for xpages

I' working on a library control for Xpages and need some help in creating.
I would create a control which reads a configuration file and creates controls in a table, controls like Editboxes, checkboxgroups and so on.
so and now to my questions:
could I initiate controls from the Exlib or must I implement them all by my self?
if I could use them from the Exlib could anyone explain me how?
I hope its clear what i mean if not please ask me for further informations.
When creating your own components, if you're closely replicating some behavior that is already in an extension library component, I highly recommend you extend that component and just add what's needed to accommodate your different functionality. This makes things much easier and you don't have to code around every little scenario that the component might be placed in.
But, if you are developing a component that is nothing like any of the extension library or core components then just ensure your component extends UIComponent or UIComponentBase. If going this route, you'll also need to create your own renderer which extends Renderer. This is what will build the on-screen representation of your component. Again, if there's already something in the core components or extension library components that closely mimics what you need then make your renderer extend that renderer. Also, don't forget to include the renderer definition in the faces-config file and the component definition in the xsp-config file or your component won't work.
As for initiating controls from the extlib.... I assume you mean can you inject them onto the page at runtime. If so the answer is absolutely yes. To add an input text field to the page where there is a container (i.e. panel, div, span, whatever) with an ID of "someContainer"
XspInputText input = new XspInputText();
input.setValue("someValue");
input.setId("someID");
UIComponent container = FacesContext.getCurrentInstance().getViewRoot().findComponent("someContainer");
container.getChildren().add(input);
To see the api for all of the core and extension library components take a look at the XPages Controls Documentation. For a more complete tutorial on creating your own components take a look at my blog for creating a custom component inside an nsf, the steps are pretty much the same for putting them into a library:
Part 1,
Part 2 and there is an example database in the Part 2 post.

Managed beans in xsp-config files

Can anyone tell me if it is possible to keep managed beans in separate .xsp-config configuration files?
And if yes than how to do this? I have tried and it only worked if I put them into faces-config.xml file.
If you want to split your bean definitions into multiple configuration files, you'll need to create an OSGi plugin. XSP libraries can contain more than one faces-config file (and you can name them whatever you want, because you specify in the library class which XML files contain faces-config definitions). But in an NSF, you're limited to just the auto-generated faces-config.xml file.
Yes, Managed beans must be defined in the faces-config.xml file.
For a good reference on all the different options within faces-config and xsp-config take a look at this website. It describes the format of pretty much everything you might ever want to add to a faces-config or xsp-config and a brief description of the options.
As far as I know they have to be in the faces-config.xml
faces-config is a JSF implementation, you should check out the JSF specification for this, there are even some good post in this forum like this one:
JSF faces config file outside WEB-INF?

Getting a path to a resource file from managed-bean in JSF

I have this situation: I am trying to remove an old avatar image for a user before putting a new one from the managed bean.
String fileName = "resources/img/useravatars/" + getSessionBean().getSearchAccount().getAvatar();
File f = new File(fileName);
I've googled a bit and it seems that I can get a path to that folder from ExternalContext like:
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext(). ...
But I couldn't find an appropriate method from class docs. Could you please help with what to put instead of ... or suggest a better solution.
PS. Somehow, I suspect it is possible to hardcode the link, but no luck so far.
I understand that the file is embedded in the WAR and that you're looking for the ExternalContext#getRealPath() method to resolve it based on a web-relative path. As per the Javadoc, this method is introduced in JSF 2.0 and does not exist in JSF 1.x. You seem to be using JSF 1.x, otherwise you wouldn't have asked this question. You need to use ServletContext#getRealPath() instead (which is also what the new JSF 2.0 method is delegating to, under the covers).
String relativeWebPath = "/resources/img/useravatars/" + ...;
ServletContext servletContext = (ServletContext) externalContext.getContext();
String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
File file = new File(absoluteDiskPath);
// ...
However, there's a big BUT: you can and should not write to the expanded WAR. Deleting files is also writing. Whenever you redeploy the WAR or restart the server, every change will be reverted and the expanded WAR will retain its initial state, hereby losing all changes made in the expanded WAR since the last deploy.
You really need to store those files in an external location whose root location can then be hardcoded or definied in some external configuration (properties) file. This way you can use java.io.File stuff the usual way.
There are several ways to serve files from an external location. You can find them all in the answer of the following question: Load images from outside of webapps / webcontext / deploy folder using <h:graphicImage> or <img> tag

Dynamic JSF resource bundle/message bundle

I would like to have a dynamic resource bundle in my application. I will show a form to the user where he can edit the value of Resource Bundle. I can’t restart my application for this changes take effect. I found a solution that solves part of my problem. Using “commons-configuration” from Apache or/and this http://www.coderanch.com/t/292347/JSP/java/we-reload-property-file , i could change my “.properties” file and get the results using “ResourceBundle.getBundle” .
The problem is when I try to access the property in my XHTML file like this:
<h:outputLabel value="#{msg[user.name]}" />
The value of this key is out of date.
Is there a solution for this?
Just for info, I am using spring, so I have this in my faces-config:
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
But I don’t think this is the problem. Anyway, another solution would be to use a session bean and get the property values accessing it.
What do you think about it? Would be better store this values in my database and forget about the properties file?
\o/
I have tested to move my resource bundle definition from my faces-config to my page using tag f:loadBundle and it worked!

Resources