Fastest way to return view in customRestService using a bean - xpages

I have written a custom rest Service on an Xpage, which is tied to a bean. The Xpage is:
<xe:restService
id="restServiceCustom"
pathInfo="custom"
ignoreRequestParams="false"
state="false"
preventDojoStore="true">
<xe:this.service>
<xe:customRestService
contentType="application/json"
serviceBean="XXXX.PCServiceBean">
</xe:customRestService>
</xe:this.service>
</xe:restService>
I cobbled together my java agent from some excellent posts around the net. I have just started on the GET. My code runs but I it seems pretty slow (on my dev server). I want to make it as fast as possible. I am using a ViewEntryCollection and I am "flushing" at each record which I assume is streaming.
I am putting my own "[" in the code, so I assume that I am not doing something right, as I never saw any examples of anyone else doing this.
Any suggestions would be greatly appreciated.
package com.XXXXX.bean;
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.openntf.domino.Database;
import org.openntf.domino.Session;
import org.openntf.domino.View;
import org.openntf.domino.ViewEntry;
import org.openntf.domino.ViewEntryCollection;
import org.openntf.domino.utils.Factory;
import com.ibm.commons.util.io.json.JsonException;
import com.ibm.commons.util.io.json.util.JsonWriter;
import com.ibm.domino.services.ServiceException;
import com.ibm.domino.services.rest.RestServiceEngine;
import com.ibm.xsp.extlib.component.rest.CustomService;
import com.ibm.xsp.extlib.component.rest.CustomServiceBean;
public class PCServiceBean extends CustomServiceBean {
#Override
public void renderService(CustomService service, RestServiceEngine engine) throws ServiceException {
try {
HttpServletRequest request = engine.getHttpRequest();
HttpServletResponse response = engine.getHttpResponse();
response.setHeader("Content-Type", "application/json; charset=UTF-8");
String method = request.getMethod();
if (method.equals("GET")) {
this.doGet(request, response);
} else if (method.equals("POST")) {
this.doPost(request, response);
} else if (method.equals("PUT")) {
this.doPut(request, response);
} else if (method.equals("DELETE")) {
this.doDelete(request, response);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void doDelete(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
}
private void doPut(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
}
private void doPost(HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub
}
private void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, JsonException {
Session session = Factory.getSession();
Database DB = session.getDatabase(session.getCurrentDatabase().getServer(), "scoApps\\PC\\PCData.nsf");
View pcView = DB.getView("viewAllByStatus");
int i = 1;
Writer out = response.getWriter();
JsonWriter writer = new JsonWriter(out, false);
writer.out("[");
ViewEntryCollection vec = pcView.getAllEntries();
int count = vec.getCount();
for (ViewEntry entry : vec) {
Vector<?> columnValues = entry.getColumnValues();
writer.startObject();
writer.startProperty("unid");
writer.outStringLiteral(String.valueOf(columnValues.get(1)));
writer.endProperty();
writer.startProperty("status");
writer.outStringLiteral(String.valueOf(columnValues.get(0)));
writer.endProperty();
writer.startProperty("assetTag");
writer.outStringLiteral(String.valueOf(columnValues.get(2)));
writer.endProperty();
writer.startProperty("serialNumber");
writer.outStringLiteral(String.valueOf(columnValues.get(3)));
writer.endProperty();
writer.startProperty("model");
writer.outStringLiteral(String.valueOf(columnValues.get(4)));
writer.endProperty();
writer.startProperty("currentLocation");
writer.outStringLiteral(String.valueOf(columnValues.get(5)));
writer.endProperty();
writer.endObject();
if (i != count) {
i = i + 1;
writer.out(",");
writer.flush();
}
}
writer.out("]");
writer.flush();
}
}

Change your code to
JsonWriter writer = new JsonWriter(out, false);
writer.startArray();
ViewEntryCollection vec = pcView.getAllEntries();
int count = vec.getCount();
for (ViewEntry entry : vec) {
Vector<?> columnValues = entry.getColumnValues();
writer.startArrayItem();
writer.startObject();
writer.startProperty("unid");
writer.outStringLiteral(String.valueOf(columnValues.get(1)));
writer.endProperty();
...
writer.endObject();
writer.endArrayItem();
}
writer.endArray();
writer.flush();
It uses JsonWriter's
startArray() and endArray() instead of out("[") and out("]")
startArrayItem() and endArrayItem() instead of out(",") and flush()
The JSON response string gets shorter if you set JsonWriter's compact option to true:
JsonWriter writer = new JsonWriter(out, true);

I see two problems.
First - use ViewNavigator. Here's good explanation of its performance gain.
https://www.mindoo.com/web/blog.nsf/dx/17.01.2013085308KLEB9S.htm
Second - prepare your JSON in advance. This is very good technique to avoid unnecessary code (and time to process it) to get JSON data from Domino documents.
https://quintessens.wordpress.com/2015/09/05/working-with-json-in-your-xpages-application/

Related

NSUrlSessionDataDelegate: Most methods don't get called

yesterday I wanted to implement the DidReceiveData method to my NSUrlSessionDataDelegate. I noticed that the DidReceiveData method never gets called, and while I tried to track down the error I noticed that the other methods don't get called either. I'm pretty sure that the other methods worked before, I don't know if I changed something or maybe a Xamarin.iOS update is the cause, but maybe I am wrong here and they never worked.
I can confirm this problem for the methods DidReceiveResponse, DidReceiveData, and DidCompleteWithError.
Here is the code that creates the NSUrlSession and handles the response (note: I call IIS WebMethods that answer in XML):
public async Task<string> Invoke(string sUrl)
{
session = NSUrlSession.FromConfiguration(NSUrlSessionConfiguration.DefaultSessionConfiguration,
(INSUrlSessionDelegate)new SessionDelegate((x)=> _fehlermeldung = x), // Übergebe Methode damit der SessionDelegate die Fehlermeldung weiterreichen kann
null);
request = CreateGETRequest(CreateNSUrl(sUrl));
var data = session.CreateDataTaskAsync(request, out dataTask);
dataTask.Resume();
NSUrlSessionDataTaskRequest response = null;
try
{
response = await data;
}
catch (NSErrorException ex)
{
throw new Exception(ex.Error.LocalizedDescription + ": " + _fehlermeldung);
}
var doc = new XmlDocument();
doc.LoadXml(response.Data.ToString());
XmlNode elem = doc.DocumentElement.FirstChild;
return elem.InnerText;
}
private NSUrl CreateNSUrl(string url)
{
string converted = ((NSString)url).CreateStringByAddingPercentEscapes(NSStringEncoding.UTF8);
var nsurl = NSUrl.FromString(converted);
if (nsurl == null)
throw new Exception("Fehlerhafte URL: Aus '" + url + "' konnte kein gültiges NSUrl Objekt erzeugt werden.");
return nsurl;
}
private NSMutableUrlRequest CreateGETRequest(NSUrl nsurl)
{
return new NSMutableUrlRequest(nsurl) { HttpMethod = "GET" };
}
And here is my implementation of the session delegate:
private class SessionDelegate : NSUrlSessionDataDelegate, INSUrlSessionDelegate
{
private readonly Action<string> setFehler;
public SessionDelegate(Action<string> setFehler)
{
this.setFehler = setFehler;
}
public override void DidReceiveResponse(NSUrlSession session, NSUrlSessionDataTask dataTask, NSUrlResponse response, Action<NSUrlSessionResponseDisposition> completionHandler)
{
var http_response = response as NSHttpUrlResponse;
if (http_response == null)
{
setFehler($"Keine HTTP Url Antwort erhalten: {Environment.NewLine}'{response}'");
completionHandler(NSUrlSessionResponseDisposition.Cancel);
return;
}
var status_code = (int)http_response.StatusCode;
if (status_code == 200)
{
completionHandler(NSUrlSessionResponseDisposition.Allow);
}
else
{
setFehler($"Verbindung abgewiesen, HTTP Status: { status_code}, '{ http_response.ToString()}'");
completionHandler(NSUrlSessionResponseDisposition.Cancel);
}
}
public override void DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data)
{
// THIS DOES NOT GET CALLED :-(
}
public override void DidCompleteWithError(NSUrlSession session, NSUrlSessionTask task, NSError error)
{
if (error != null)
{
task.Cancel();
var ex = new NSErrorException(error);
throw (ex);
}
}
public override void DidReceiveChallenge(NSUrlSession session, NSUrlSessionTask task, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
{
[...] // This is working fine for Client Cert Authentication or Basic Authentication
}
}
The DidReceiveChallenge method does get called and is working. I cut it out for readability.
All the other methods don't get called. I would like to know what I am doing wrong. You can find the same questions from native iOS users, but I can't figure out how to implement their solutions with Xamarin.iOS.
Any help is appreciated, thanks in advance.

Commons Configuration2 ReloadingFileBasedConfiguration

I am trying to implement the Apache Configuration 2 in my codebase
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;
import org.apache.commons.configuration2.CompositeConfiguration;
public class Test {
private static final long DELAY_MILLIS = 10 * 60 * 5;
public static void main(String[] args) {
// TODO Auto-generated method stub
CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
PropertiesConfiguration props = null;
try {
props = initPropertiesConfiguration(new File("/tmp/DEV.properties"));
} catch (ConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
compositeConfiguration.addConfiguration( props );
compositeConfiguration.addEventListener(ConfigurationBuilderEvent.ANY,
new EventListener<ConfigurationBuilderEvent>()
{
#Override
public void onEvent(ConfigurationBuilderEvent event)
{
System.out.println("Event:" + event);
}
});
System.out.println(compositeConfiguration.getString("property1"));
try {
Thread.sleep(14*1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Have a script which changes the value of property1 in DEV.properties
System.out.println(compositeConfiguration.getString("property1"));
}
protected static PropertiesConfiguration initPropertiesConfiguration(File propsFile) throws ConfigurationException {
if(propsFile.exists()) {
final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
.configure(new Parameters().fileBased()
.setFile(propsFile)
.setReloadingRefreshDelay(DELAY_MILLIS)
.setThrowExceptionOnMissing(false)
.setListDelimiterHandler(new DefaultListDelimiterHandler(';')));
final PropertiesConfiguration propsConfiguration = builder.getConfiguration();
PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
null, 1, TimeUnit.SECONDS);
trigger.start();
return propsConfiguration;
} else {
return new PropertiesConfiguration();
}
}
}
Here is a sample code that I using to check whether the Automatic Reloading works or not. However when the underlying property file is updated, the configuration doesn't reflect it.
As per the documentation :
One important point to keep in mind when using this approach to reloading is that reloads are only functional if the builder is used as central component for accessing configuration data. The configuration instance obtained from the builder will not change automagically! So if an application fetches a configuration object from the builder at startup and then uses it throughout its life time, changes on the external configuration file become never visible. The correct approach is to keep a reference to the builder centrally and obtain the configuration from there every time configuration data is needed.
https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html#Reloading_File-based_Configurations
This is different from what the old implementation was.
I was able to successfully execute your sample code by making 2 changes :
make the builder available globally and access the configuration from the builder :
System.out.println(builder.getConfiguration().getString("property1"));
add the listener to the builder :
`builder.addEventListener(ConfigurationBuilderEvent.ANY, new EventListener() {
public void onEvent(ConfigurationBuilderEvent event) {
System.out.println("Event:" + event);
}
});
Posting my sample program, where I was able to successfully demonstrate it
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;
public class TestDynamicProps {
public static void main(String[] args) throws Exception {
Parameters params = new Parameters();
ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
.configure(params.fileBased()
.setFile(new File("src/main/resources/override.properties")));
PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
null, 1, TimeUnit.SECONDS);
trigger.start();
builder.addEventListener(ConfigurationBuilderEvent.ANY, new EventListener<ConfigurationBuilderEvent>() {
public void onEvent(ConfigurationBuilderEvent event) {
System.out.println("Event:" + event);
}
});
while (true) {
Thread.sleep(1000);
System.out.println(builder.getConfiguration().getString("property1"));
}
}
}
The problem with your implementation is, that the reloading is done on the ReloadingFileBasedConfigurationBuilder Object and is not being returned to the PropertiesConfiguration Object.

Creating a java client for secured esb proxy

I want to create a java client for version proxy service present in wso2 esb. I have secured the version proxy with Username Token Authentication scenario. Now i have started creating the java client to invoke this secured proxy service and my client code is:
package org.wso2.carbon.security.ws;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;
import org.apache.axis2.description.AxisBinding;
import org.apache.axis2.description.AxisEndpoint;
import org.apache.axis2.rpc.client.RPCServiceClient;
import org.apache.neethi.Policy;
import javax.xml.namespace.QName;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Map;
public class HelloServiceClient {
static {
System.setProperty("javax.net.ssl.trustStore", "/path/to/keystore" + File.separator+ "wso2carbon.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "wso2carbon");
}
public static void main(String[] args) {
try {
int securityScenario = getSecurityScenario();
String repository = "/path/to/repo" + File.separator + "repository";
ConfigurationContext confContext =
ConfigurationContextFactory.
createConfigurationContextFromFileSystem(repository, null);
String endPoint = "HelloServiceHttpSoap12Endpoint";
if(securityScenario == 1){
endPoint = "HelloServiceHttpsSoap12Endpoint"; // scenario 1 uses HelloServiceHttpsSoap12Endpoint
}
RPCServiceClient dynamicClient =
new RPCServiceClient(confContext,
new URL("http://pc213712:8281/services/Version?wsdl"),
new QName("http://version.services.core.carbon.wso2.org", "Version"),
endPoint);
//Engage Modules
dynamicClient.engageModule("rampart");
dynamicClient.engageModule("addressing");
//TODO : Change the port to monitor the messages through TCPMon
if(securityScenario != 1){
dynamicClient.getOptions().setTo(new EndpointReference("http://pc213712:8281/services/Version/"));
}
//Get the policy from the binding and append the rampartconfig assertion
Map endPoints = dynamicClient.getAxisService().getEndpoints();
AxisBinding axisBinding = ((AxisEndpoint) endPoints.values().iterator().next()).getBinding();
Policy policy = axisBinding.getEffectivePolicy();
**policy.addAssertion(RampartConfigBuilder.createRampartConfig(securityScenario));**
axisBinding.applyPolicy(policy);
//Invoke the service
Object[] returnArray = dynamicClient.invokeBlocking(new QName("http://www.wso2.org/types","greet"),
new Object[]{"Alice"},
new Class[]{String.class});
System.out.println((String) returnArray[0]);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static int getSecurityScenario() {
int scenarioNumber = 0;
while (scenarioNumber < 1 || scenarioNumber > 15) {
System.out.print("Insert the security scenario no : ");
String inputString = readOption();
try {
scenarioNumber = new Integer(inputString);
} catch (Exception e) {
System.out.println("invalid input, insert a integer between 1 and 15");
}
if(scenarioNumber < 1 || scenarioNumber > 15){
System.out.println("Scenario number should be between 1 and 15");
}
}
return scenarioNumber;
}
private static String readOption() {
try {
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = console.readLine()).equals("")) {
}
return str;
} catch (Exception e) {
return null;
}
}
}
But in the above code i am struck at one line that is :
policy.addAssertion(RampartConfigBuilder.createRampartConfig(securityScenario));
Here in my rampart_core jar I am getting RampartConfigBuilder class but inside this class there is no such method called createRampartConfig. So i am unable to create Rampart configurations. What can i do to solve this issue? looking forward to your solutions. Thanks in advance

sending image from one mobile to another via sms using j2me language

i am programming a mobile application in j2me in which i need to send an image from one mobile to another via sms.
the problem is being encountered at the receiving end.the image is not being decoded properly.it is throwing ioexception....i m posting the code here..plz help me.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;
import javax.microedition.lcdui.game.*;
import java.lang.*;
import java.io.*;
public class receive_mms extends MIDlet implements CommandListener
{
Display disp;
//TextBox txtbox;
MessageConnection msgConn;
Message msg;
Form frm=null;
byte[] msgrev;
byte[] data;
//String msgrev;
Image im=null;
Image im1=null;
ImageItem img=null;
int i,j;
ByteArrayInputStream bais = null;
Command cmd_exit;
public receive_mms(){
disp=Display.getDisplay(this);
frm=new Form("photo dikho");
i=frm.getWidth();
j=frm.getHeight();
cmd_exit=new Command("exit",Command.EXIT,1);
frm.addCommand(cmd_exit);
frm.setCommandListener(this);
disp.setCurrent(frm);
Thread t1 = new Thread()
{
public void run()
{recieve();}
};
t1.start();
//txtbox=new TextBox("Recieve Text","",100,TextField.ANY);
}
public void commandAction(Command c,Displayable d)
{
if(c==cmd_exit)
{
notifyDestroyed();
}
}
public void startApp(){/*
disp.setCurrent(frm);
Thread t1 = new Thread()
{
public void run()
{recieve();}
};
t1.start();
*/
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void recieve(){
//while(true)
//{
String mSenderAddress="";
try{
msgConn = (MessageConnection) Connector.open("sms://:1234");
System.out.println("11");
msg = msgConn.receive();// start listening and stuck here until a msg is received
System.out.println("12");
mSenderAddress = msg.getAddress();// Get info from message, from where da msg is beign sent
System.out.println("3");
System.out.println("add"+ mSenderAddress);
System.out.println("msg aya:" + msg);
msgConn.close();
}catch(Exception e){System.out.println(e);}
if (msg instanceof BinaryMessage) {
//try{
msgrev = ((BinaryMessage)msg).getPayloadData();
data=msgrev.toByteArray();
String val= new String(data);
System.out.println("yahoo");
System.out.println("yahoo1");
System.out.println(val);
create(data);
}
}
public void create(byte[] bs)
{
try
{
String str=bs.toString();
/*
StringBuffer d=new StringBuffer();
bais=new ByteArrayInputStream(bs);
DataInputStream ds=new DataInputStream(bais);
int len=bs.length;
System.out.println("len="+len);
if(len!=0)
{
int ch=0;
while((ch=ds.read())!=-1)
{
d.append((char)ch);
}
}
System.out.println(d);
str=d.toString();
*/
//str=bs.toString();
InputStream is= this.getClass().getResourceAsStream(str);
System.out.println("string is"+str);
im = (Image)Image.createImage(is);
System.out.println("line");
im1 = (Image)Image.createImage(im, 0, 0, i, j, Sprite.TRANS_NONE);
img = new ImageItem("yeh photo snd hui", im1, Item.LAYOUT_CENTER, "kyu nhi dikh rhi", Item.BUTTON);
frm.append(img);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
You're doing a few very odd things:
converting the byte array to a String, particularly using byte[].toString()
attempting to get an InputStream by calling Class.getResourceAsStream() with a String that has been created from the byte array.
using SMS to send an Image
Class.getResourceAsStream() is intended to take a String identifying a resource file within the MIDlet's jar file.
The correct way to do this is to get the byte[] from the BinaryMessage and use this to create an Image using Image.createImage(bytes, 0, bytes.length);
Although, as you're sending it using SMS, I'd hope it was a very small image indeed or anybody using this app will incur high costs from splitting a large image over several SMSs. Beware also that some networks limit the number of parts that an SMS can be split into.
You would be much better off researching the MMS sending functionality provided by JSR 205.
You are getting wrong the data stream, here is how you must do it:
public void create(byte[] bs)
{
try
{
im = (Image)Image.createImage(bs, 0, bs.length);
im1 = (Image)Image.createImage(im, 0, 0, i, j, Sprite.TRANS_NONE);
img = new ImageItem("yeh photo snd hui", im1, Item.LAYOUT_CENTER, "kyu nhi dikh rhi", Item.BUTTON);
frm.append(img);
}
catch (Exception e)
{
System.out.println(e);
}
}
This should work.

Use argument in EL expression

I need to use function with argument in a EL expression (with JSF) like this:
<h:outputText value="#{object.test(10)}" ></h:outputText>
But it doesn't work.
I read on the web that it's impossible to do this with JSF. I use facelet with JSF.
Someone knows how to do that ?
Thanks.
You could provide the method as a custom facelet function in your own taglib. The method must be static, so if you are trying to call a method on a specific bean, you would have to pass the bean, and the parameters to your static facelet function. In your case, it would be something like
<h:outputText value="#{my:doStuff(object,10)}" ></h:outputText>
and your facelet function would be
public static String doStuff( MyType o, int param )
{
return o.test( param );
}
Then, using the information in the facelets docbook you would define your function in your taglib.xml file.
It's not the prettiest solution, especially if you plan on doing this a lot, but I believe the next version of the EL (in java EE 6) will allow for using parameters in some cases.
Edit: Some info about parameterized method calls in the next version of el can be found on Ryan Lubke's Blog
I find a sad solution but it's working. I overload a map like this:
new AbstractMap<Integer, String>()
{
#Override
public Set<Entry<Integer, String>> entrySet()
{
return null;
}
#Override
public String get(final Object arg0)
{
Integer keywordDb = (Integer)arg0;
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
HashMap<String, String> params = new HashMap<String, String>();
params.put("keywordDb", keywordDb.toString());
params.put("month", new Integer(cal.get(Calendar.MONTH) + 1).toString());
params.put("year", new Integer(cal.get(Calendar.YEAR)).toString());
DataAnalyzeManager manager = new DataAnalyzeManager();
manager.setEm(modelPosition.getEm());
DataAnalyze data = manager.findDataByParams(params, modelPosition.getSite(), false, DataAnalyzeManager.VISITBYMONTHBYKEYWORD);
if (data != null)
return data.getDataInt().toString();
return "0";
}
};
Thereby, I can do that in my JSF:
#{homePositionController.visitByMonth[keyword.keyword.keywordDb]}
And my function is executed.
You may have to have <%# page isELIgnored ="false" %>
at the top of your pages. Read more here. The default is to ignore el expressions. What version of the JSP spec are you using with JSF? If you are using JSF 2 with JSP < 2.1 you are going to run into problems.
Also, what version of el are you using? You can't pass method params with older versions.
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.1.2-b05</version>
</dependency>
There is couple ways about doing that, you could use JBoss EL expression implementation they support method calls with parameters check out Seam, or use similar approach as #digitaljoel suggested.
This is what I created for that purpose, you can call static and static methods, not a great solution but it does the job.
<c:if test="#{t:call(null, '#Util.SecurityUtility', 'isPanelWorkbookEnabledForUser','')}">
Hello Panel
</c:if>
#Util is just an alias to com.mycomp.util where
Example 2
<c:if test="#{item != null and t:call(item, 'java.lang.String', 'indexOf', t:params(t:param('flash-alert',''))) == 0}">
#{t:call(session, 'org.apache.catalina.session.StandardSessionFacade', 'removeAttribute', t:params(t:param(item,'')))}
</c:if>
Syxtax
java.lang.Object call(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object[])
Where Object is object we want to invoke method on, String is the method name, Object[] are parameters to pass.
t:call, t:params, t:param are function defined in project-taglib.xml as so
<function>
<function-name>call</function-name>
<function-class>util.Functions</function-class>
<function-signature>java.lang.Object call(java.lang.Object, java.lang.String, java.lang.String, java.lang.Object[])</function-signature>
</function>
<function>
<function-name>param</function-name>
<function-class>.util.Functions</function-class>
<function-signature>java.lang.String param(java.lang.Object, java.lang.String)</function-signature>
</function>
<function>
<function-name>params</function-name>
<function-class>util.Functions</function-class>
<function-signature>java.lang.Object[] params(java.lang.String)</function-signature>
</function>
Here is the implementation
package mycompany.web.util;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.StringWriter;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.el.MethodNotFoundException;
public class Functions {
private static HashMap<String, String> alliasMap;
static{
alliasMap=new HashMap<String, String>();
alliasMap.put("#DateUtil", "com.americanbanksystems.compliance.util.DateUtil");
//Match anything following the dot(.)
alliasMap.put("#Util.*", "com.americanbanksystems.compliance.util");
alliasMap.put("#Application.*", "com.americanbanksystems.compliance.application");
}
public static String param(Object obj, String cls) {
//make sure that passed in object is not null
if(obj==null){
obj="";
}
ByteArrayOutputStream baut=new ByteArrayOutputStream();
XMLEncoder encoder=new XMLEncoder( baut );
//Bug in the JDK
//http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=c993c9a3160fd7de44075a2a1fa?bug_id=6525396
if(obj instanceof java.sql.Timestamp){
Date o = new Date(((java.sql.Timestamp)obj).getTime());
obj=o;
}
//Checking if this is possible
if(String.class.isAssignableFrom(obj.getClass())){
//removed trailing +" " because it was causing indexOf return invalid value
//Unknown side effects
obj=FacesUtil.get(obj.toString());
}
encoder.writeObject( obj );
encoder.close();
return new String(baut.toByteArray());
}
private static Object decode(String str){
ByteArrayInputStream bais=new ByteArrayInputStream(str.getBytes());
XMLDecoder decoder=new XMLDecoder(bais);
return decoder.readObject();
}
public static Object[] params(String str){
// (?<=</java>)\s*(?=<?)
String[] obj=str.split("(?<=</java>)\\s*(?=<?)");
Object[] results=new Object[obj.length];
for(int i=0;i<obj.length;i++){
results[i]=decode(obj[i]);
}
return results;
}
#SuppressWarnings("unchecked")
public static Object call(Object owningObject, String qualifiedClassname, String methodName, java.lang.Object... methodArguments) {
if (null == methodName || methodName.equals("")) {
throw new IllegalArgumentException("Method name can't be null or empty");
}
if (null == methodArguments) {
methodArguments = new Object[0];
}
//Check for aliases
if(qualifiedClassname.indexOf("#")>-1){
String subpackage=qualifiedClassname;
String originalClass=qualifiedClassname;
//Split at the dot
boolean isPackageAllias=false;
String[] sp=subpackage.split("\\.");
if(sp.length>1){
subpackage=sp[0]+".*";
isPackageAllias=true;
}
if(alliasMap.containsKey(subpackage)){
String value = alliasMap.get(subpackage);
if(isPackageAllias){
qualifiedClassname=subpackage.replace(sp[0], value);
qualifiedClassname=qualifiedClassname.replace(".*", originalClass.replace(sp[0],""));
}else{
qualifiedClassname=value;
}
}else{
throw new IllegalArgumentException("Allias name '"+qualifiedClassname+"' not found");
}
}
Class clazz;
try {
clazz = Class.forName(qualifiedClassname);
//Find method by methodName,Argument Types
Class[] argumentTypes=new Class[methodArguments.length];
for(int i=0;i<methodArguments.length;i++){
argumentTypes[i]=methodArguments[i].getClass();
//Check if the passed in method argument is a string and if its represented as unicode char
//if it is then convert it into a char and reassign to the original parameter
//example 1: \u0022 == "
//example 2: \u0027 == '
// Reason for this functionality is that we can't pass " and ' from within t:call method
if (argumentTypes[i] == String.class && methodArguments[i].toString().indexOf("\\u") > -1) {
String arg = methodArguments[i].toString();
arg = arg.substring(2, arg.length());
try {
int outchar = Integer.parseInt(arg, 16);
if (Character.isDefined(outchar)) {
methodArguments[i] = String.valueOf((char) outchar);
}
} catch (NumberFormatException nfe) {
// Suppress error and continue assuming this is a regular string
}
}
}
Method methodToInvoke = null;
try{
methodToInvoke = clazz.getMethod(methodName, argumentTypes);
}catch(NoSuchMethodException nsm){//Find by method name/ argument count
for (Method method : clazz.getMethods()) {
if (method.getName().equals(methodName) && method.getParameterTypes().length == methodArguments.length) {
if (null == owningObject) {
owningObject = clazz.newInstance();
}
methodToInvoke=method;
break;
}
}
}
if(methodToInvoke!=null){
return methodToInvoke.invoke(owningObject, methodArguments);
}else{
throw new InstantiationException("method not found :" + methodName);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] arg) {
// StringBuffer buff=new StringBuffer();
// buff.append("Gregs init");
// Functions.call(java.lang.Class<T>, T, java.lang.String, java.lang.String, java.lang.Object...)
/*
* Functions.call(StringBuffer.class, buff, "java.lang.StringBuffer","append"," Init ");
* Functions.call(StringBuffer.class, buff, "java.lang.StringBuffer","append"," greg ");
* System.out.println("output="+ buff);
*/
//#{t:call(null, ".util.DateUtil", "normalizeDate", t:parametize(editRiskActionPlan.riskActionPlan.completionDate,",","java.lang.Object"))}
// c(call(null, "util.DateUtil", "normalizeDate", new Date()));
// #{t:parametize(editRiskActionPlan.riskActionPlan.completionDate,",","java.lang.Object")}
//parametize((new Date()).toString(),",","java.lang.Object");
Date a=new Date();
Date b=new Date();
String rawString=param((Date)b, Date.class.toString() );
//System.out.println(rawString);
//Replaced=#{t:call("Gregs ' car", 'java.lang.String', 'replace', t:params( parameter ))}
String paramA=param("\\u0027","");
String paramB=param("\\u0022","");
String params=paramA+paramB;
String in="I need to ' have a replaced single quote with double";
String out=(String)call(in, "java.lang.String", "replace", params(params));
System.out.println(out);
/*
Object[] obj=params(rawString);
for(Object o:obj){
System.out.println(o);
}
//c(call(null, "#DateUtil", "normalizeDate", obj));
*/
}
}
I hope this helps, btw this was copied/pasted from my project so not sure if I missed anything.

Resources