i have written a staf service, when i submit a request from the command line with a parameter in regular expression,such as [^(no)]+error . but when i use the method submit2() in a java program to submit the request, it returned different result! i print out the resolved paramter string and find the [^(no)] is changed to [(no)] from command line, but [^(no)] is still [^(no)]. what should i do?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
This worked fine when I tried it. I used the VAR RESOLVE request so that it would resolve any STAF variables in the string. Is your STAF service resolving variables in the parameter's value? Let me know if it is or isn't. For me, the result was the same when submitting the same VAR RESOLVE STRING "[^(no)] msg" request from the command line or from a Java program via submit2().
Here's a java program that I wrote that shows the same results as when the same requests are submitted from the command line.
import com.ibm.staf.*;
public class TestEnv {
public static void main (String[] args) {
new TestEnv();
}
C:\>staf local var resolve string "[^(no)] msg"
Response
--------
[^(no)] msg
C:\>staf local var resolve string "[^{no}] msg"
Response
--------
[{no}] msg
C:\>staf local var resolve string "[^^^{no}] msg"
Response
--------
[^{no}] msg
RESOLVE allows you to have all variable references in a string resolved to their values. A variable reference is denoted by surrounding the variable in curly braces, for example, {WebServer}.
Because of this special significance of "{", if you do not want variable substitution performed, use a caret, "^", as an escape character for "{" and "^".
You said your STAF service is "eating" the caret (^) when you specify string "[^(no)]". Are you sure you aren't using a { instead of a ( ? If so, and the service is performing STAF variable resolution, then the expected resolved value for "[^{no}]" would be "[{no}]". You need to escape the ^ and { it as I showed in my examples: Specify "[^^^{no}]" which resolves to "[^{no}]". Or, more likely if regular expressions are commonly going to be specified in the value for this parameter, perhaps your service should not resolve variables in this value so that escaping is not needed.
If you really aren't using the ^{ instead of ^(, please provide an example of exactly what you're doing.
Also, what version of STAF are you using?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
thank you every much for your reply!
i can give an example, i think it's the problem of
res = STAFUtil.resolveRequestVar(parsedResult.optionValue("char"),handle, info.requestNumber);
The problem when submitting your request from the Windows command prompt is due to the Windows command "eating" the caret. You simply need to include values containing a caret in double quotes when submitting a command from a Windows command prompt.
For example, the following "eats" the caret:
C:\>staf local TestChar char [^(no)]
Response
--------
[(no)]
Using quotes shows the result you expected:
C:\>staf local TestChar char "[^(no)]"
Response
--------
[^(no)]
Note that this isn't a "STAF" thing, but a Windows command parser thing.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
i have written a staf service, when i submit a request from the command line with a parameter in regular expression,such as [^(no)]+error . but when i use the method submit2() in a java program to submit the request, it returned different result! i print out the resolved paramter string and find the [^(no)] is changed to [(no)] from command line, but [^(no)] is still [^(no)]. what should i do?
This worked fine when I tried it. I used the VAR RESOLVE request so that it would resolve any STAF variables in the string. Is your STAF service resolving variables in the parameter's value? Let me know if it is or isn't. For me, the result was the same when submitting the same VAR RESOLVE STRING "[^(no)] msg" request from the command line or from a Java program via submit2().
Here's a java program that I wrote that shows the same results as when the same requests are submitted from the command line.
import com.ibm.staf.*;
public class TestEnv {
public static void main (String[] args) {
new TestEnv();
}
public TestEnv() {
STAFHandle handle;
try {
handle = new STAFHandle("WebTestCentre");
STAFResult res;
res = handle.submit2("local", "VAR", "RESOLVE STRING [^(no)] " + "msg");
System.out.println("RC=" + res.rc + " STAFResult=" + res.result);
res = handle.submit2("local", "VAR", "RESOLVE STRING [^{no}] " + "msg");
System.out.println("RC=" + res.rc + " STAFResult=" + res.result);
res = handle.submit2("local", "VAR", "RESOLVE STRING [^^^{no}] " + "msg");
System.out.println("RC=" + res.rc + " STAFResult=" + res.result);
handle.unRegister();
} catch (STAFException e) {
System.out.println("STAFException creating handle: " + e.getMessage());
}
}
}
Here are the results when running the Java program and when submitting the same requests via the command line and the results are the same:
C:\>java TestEnv
RC=0 STAFResult=[^(no)] msg
RC=0 STAFResult=[{no}] msg
RC=0 STAFResult=[^{no}] msg
C:\>staf local var resolve string "[^(no)] msg"
Response
--------
[^(no)] msg
C:\>staf local var resolve string "[^{no}] msg"
Response
--------
[{no}] msg
C:\>staf local var resolve string "[^^^{no}] msg"
Response
--------
[^{no}] msg
RESOLVE allows you to have all variable references in a string resolved to their values. A variable reference is denoted by surrounding the variable in curly braces, for example, {WebServer}.
Because of this special significance of "{", if you do not want variable substitution performed, use a caret, "^", as an escape character for "{" and "^".
You said your STAF service is "eating" the caret (^) when you specify string "[^(no)]". Are you sure you aren't using a { instead of a ( ? If so, and the service is performing STAF variable resolution, then the expected resolved value for "[^{no}]" would be "[{no}]". You need to escape the ^ and { it as I showed in my examples: Specify "[^^^{no}]" which resolves to "[^{no}]". Or, more likely if regular expressions are commonly going to be specified in the value for this parameter, perhaps your service should not resolve variables in this value so that escaping is not needed.
If you really aren't using the ^{ instead of ^(, please provide an example of exactly what you're doing.
Also, what version of STAF are you using?
thank you every much for your reply!
i can give an example, i think it's the problem of
res = STAFUtil.resolveRequestVar(parsedResult.optionValue("char"),handle, info.requestNumber);
the service code as follow:
package TestService;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import com.ibm.staf.STAFException;
import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFResult;
import com.ibm.staf.STAFUtil;
import com.ibm.staf.service.STAFCommandParseResult;
import com.ibm.staf.service.STAFCommandParser;
import com.ibm.staf.service.STAFServiceInterfaceLevel30;
public class TestChar implements STAFServiceInterfaceLevel30 {
private STAFHandle handle;
private String myServiceName;
private String myLocalMachineName;
private STAFCommandParser charParser;
public TestChar() {
}
/**
* init
*/
public STAFResult init(STAFServiceInterfaceLevel30.InitInfo info) {
// register the service
try {
myServiceName = info.name;
handle = new STAFHandle("STAF/Service" + info.name);
} catch (STAFException e) {
return new STAFResult(STAFResult.STAFRegistrationError, e
.toString());
}
// resolve the machine name
STAFResult res = STAFUtil.resolveInitVar("{STAF/Config/Machine}",
handle);
if (res.rc != STAFResult.Ok)
return res;
myLocalMachineName = res.result;
// add the command parser
charParser = new STAFCommandParser(0, false);
charParser.addOption("char", 1, STAFCommandParser.VALUEREQUIRED);
return new STAFResult(STAFResult.Ok);
}
public STAFResult acceptRequest(STAFServiceInterfaceLevel30.RequestInfo info) {
// chech the trust level
STAFResult trustResult = STAFUtil.validateTrust(5, myServiceName,
"char", myLocalMachineName, info);
if (trustResult.rc != STAFResult.Ok)
return trustResult;
// choose the handlecommand
String requestStr = info.request.toLowerCase();
StringTokenizer token = new StringTokenizer(requestStr);
String request = token.nextToken();
if (request.equals("char")) {
return handleChar(info);
} else {
return new STAFResult(STAFResult.InvalidRequestString,
"Unknown DeviceService Request: " + requestStr);
}
}
public STAFResult handleChar(STAFServiceInterfaceLevel30.RequestInfo info) {
STAFCommandParseResult parsedResult = charParser.parse(info.request);
STAFResult res = new STAFResult();
res = STAFUtil.resolveRequestVar(parsedResult.optionValue("char"),
handle, info.requestNumber);
if (res.rc != STAFResult.Ok)
return res;
String param = res.result;
//*****************output info
try {
FileWriter fw=new FileWriter("c:\\staf\\test.txt",true);
fw.write("char "+param+"\\n");
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//****************output info end
return new STAFResult(STAFResult.Ok, param);
}
public STAFResult term() {
try {
handle.unRegister();
} catch (STAFException e) {
// TODO Auto-generated catch block
return new STAFResult(STAFResult.STAFRegistrationError, e
.toString());
}
return new STAFResult(STAFResult.Ok);
}
}
so when i use the command line to request this service the result is:
D:\test>staf local TestChar char [^(no)]
Response
--------
[(no)]
but when i use the java program of :
package TestService;
import com.ibm.staf.STAFException;
import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFResult;
public class SubChar {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
STAFHandle handle = null;
try
{
handle = new STAFHandle("LogMinner");
}
catch (STAFException e)
{
System.out.println("Error registering with STAF, RC: " + e.rc);
System.exit(1);
}
STAFResult rs=handle.submit2("local","TestChar","char [^(no)]");
System.out.println("result "+rs.result);
try
{
handle.unRegister();
}
catch (STAFException e)
{
System.out.println("Error unregistering with STAF, RC: " + e.rc);
System.exit(1);
}
}
}
to submit a request
the result like this:
result [^(no)]
so i am confused! could you help me? thank you every much!!!
The problem when submitting your request from the Windows command prompt is due to the Windows command "eating" the caret. You simply need to include values containing a caret in double quotes when submitting a command from a Windows command prompt.
For example, the following "eats" the caret:
C:\>staf local TestChar char [^(no)]
Response
--------
[(no)]
Using quotes shows the result you expected:
C:\>staf local TestChar char "[^(no)]"
Response
--------
[^(no)]
Note that this isn't a "STAF" thing, but a Windows command parser thing.