I want to update xml data and write it in flowfile1 but for some reason my ExecuteScript processor can't specify transfer relationship here is my code, what should i change to make this task?:
Is it possbile that my code inside session.write can't cast xml data to ByteArray and can't write this in flowfile content? ( but it doesn't throw exception)
flowFile1 = session.putAttribute(flowFile1, "filename", "conf.xml");
session.write(flowFile1, new StreamCallback() {
#Override
public void process(InputStream inputStream1, OutputStream outputStream) throws IOException {
TransformerFactory transformerFactory1 = TransformerFactory.newInstance();
Transformer transformer1 = null;
try {
transformer1= transformerFactory1.newTransformer();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
DOMSource source1 = new DOMSource(doc);
ByteArrayOutputStream bos1 = new ByteArrayOutputStream();
StreamResult result = new StreamResult(bos1);
try {
transformer1.transform(source1, result);
} catch (TransformerException e) {
e.printStackTrace();
}
byte[] array1 = bos1.toByteArray();
outputStream.write(array1);
}
});
if(flowFile1!=null){
session.transfer(flowFile1, REL_SUCCESS);
}
else{
session.transfer(flowFile1, REL_FAILURE);
}
}catch (OverlappingFileLockException e) {
lock.release();
}
catch (FileNotFoundException e) {
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}finally {
lock.release();
ini.close();
}
session.write() returns a reference to a newer version of the flow file, but you are not storing it or transferring it. Later on, you end up trying to transfer a version that is not the latest. Try adding "flowFile1 = " to the beginning of your session.write() statement.
Im trying to remove usergroup from role using the following method. But it doesn't work. Can some one help me to identify the problem?
public static boolean deleteUserGroupFromRole( String groupName, String roleName )
{
try
{
company = CompanyLocalServiceUtil.getCompanyByMx( PropsUtil.get( PropsKeys.COMPANY_DEFAULT_WEB_ID ) );
long companyId = company.getCompanyId();
UserGroup lportalUserGroup= SoasLportalGroupHelper.getLportalUserGroup( groupName);
Role role= getRole( companyId, roleName );
GroupLocalServiceUtil.deleteRoleGroup(role.getRoleId(), lportalUserGroup.getGroupId() );
logger.debug( "Role : "+roleName +" has been deleted from groupName "+groupName);
return true;
}
catch ( PortalException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch ( SystemException e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
I want to make an android-json request to a webservice with httpClient.
The method which i try to call is "authenticate"
The request should have the following structure:
{"id":"ID","method":"authenticate","params":{"user":"ANDROID",
"password":"PASSWORD", "client":"CLIENT"},"jsonrpc":"2.0"}
mandatory parameter: ?school=SCHOOLNAME
This is what i have tried:
class MyAsnycTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... params) {
String apiUrl = "https://arche.webuntis.com/WebUntis/jsonrpc.do";
JSONObject jsonParams = new JSONObject();
JSONObject params1 = new JSONObject();
HttpClient client = new DefaultHttpClient();
// Prepare a request object
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-type", "application/json");
try {
params1.put("?school","litec");
params1.put("user", "40146720133271");
params1.put("password", "1234567");
jsonParams.put("id", "ID");
jsonParams.put("method", "authenticate");
jsonParams.put("params", params1);
jsonParams.put("jsonrpc", "2.0");
StringEntity se = new StringEntity(jsonParams.toString());
post.setEntity(se);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Execute the request
try {
HttpResponse response = client.execute(post);
Log.d("log_response: ", response.getStatusLine().toString());
// Get hold the response entity
HttpEntity entity = response.getEntity();
// if the response does not enclose the entity, there is no need
// to worry about it
if(entity != null){
// a simple JSON Response read
InputStream instream = entity.getContent();
String result;
// convert content of response to bufferedreader
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
instream.close();
}catch(IOException exp){
exp.printStackTrace();
}
}
result = sb.toString();
Log.d("Result of the Request: ", result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "OK";
}
protected String doInBackground(String result) {
return result;
// TODO Auto-generated method stub
}
}
After executing this, i get the request:
{"jsonrpc":"2.0","id":"error","error":{"message":"invalid schoolname","code":-8500}}
So its telling me that our schoolname is false.
So what can i do, is my way to pass parameters wrong ?
I saw your question some time ago, but I was not able to answer. I'm working with the WebUntis API as well, and I dont know if you solved the error, but it is a simple error in the url. As mentionend in the API, the mandatory paramter for the method 'authenthicate' is ?school=SCHULNAME. Your Url in the code is 'https://arche.webuntis.com/WebUntis/jsonrpc.do', but the mandatory parameter SCHULNAME is not given. Your Url should look like this: https://arche.webuntis.com/WebUntis/jsonrpc.do?school=SCHULNAME. Maybe you have to add the length of your request. E.g. if you use the method authenthicate: {"id":"ID","method":"authenticate","params":{"user":"USR", "password":"PW", "client":"MyApp"},"jsonrpc":"2.0"}
In this case length would be 109. I hope this helped, even if the question is over a month old. For other Googlers: If you are not using an AsyncTask, you have to return true, not ok.
EDIT:
The code looks like this (I haven't tested this yet, I hope it works):
class MyAsnycTask extends AsyncTask<String, String, String>{
protected String doInBackground(String... params) {
String apiUrl = "https://arche.webuntis.com/WebUntis/jsonrpc.do?school=SCHULNAME"; //Changes here
JSONObject jsonParams = new JSONObject();
JSONObject params1 = new JSONObject();
HttpClient client = new DefaultHttpClient();
// Prepare a request object
HttpPost post = new HttpPost(apiUrl);
post.setHeader("Content-type", "application/json");
try {
params1.put("user", "40146720133271");
params1.put("password", "1234567");
params1.put("client", "seriouslysAndroidApp"); //You can change the name
jsonParams.put("id", "ID");
jsonParams.put("method", "authenticate");
jsonParams.put("params", params1);
jsonParams.put("jsonrpc", "2.0");
StringEntity se = new StringEntity(jsonParams.toString());
post.setHeader("Content-length",""+se.getContentLength()); //header has to be set after jsonparams are complete
post.setEntity(se);
} catch (JSONException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Execute the request
try {
HttpResponse response = client.execute(post);
Log.d("log_response: ", response.getStatusLine().toString());
// Get hold the response entity
HttpEntity entity = response.getEntity();
// if the response does not enclose the entity, there is no need
// to worry about it
if(entity != null){
// a simple JSON Response read
InputStream instream = entity.getContent();
String result;
// convert content of response to bufferedreader
BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null){
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
instream.close();
}catch(IOException exp){
exp.printStackTrace();
}
}
result = sb.toString();
Log.d("Result of the Request: ", result);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "OK";
}
protected String doInBackground(String result) {
return result;
// TODO Auto-generated method stub
}
Here is a method to do that, but I'm not sure if it is a reasonable way.
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
CatalogManager.getStaticManager().setIgnoreMissingProperties(true);
final CatalogResolver entityResolver = new CatalogResolver(true);
try {
entityResolver.getCatalog().parseCatalog(new URL("file:///catalog.cat"));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sf.setResourceResolver(new LSResourceResolver() {
#Override
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
if (publicId == null) {
publicId = namespaceURI;
}
return new LSInputSAXWrapper(entityResolver.resolveEntity(publicId, systemId));
}
});
HI ALL,
I am working in j2me midp2.0 environment.My application wants to read a text file which is stored in mobile device.How to read the text file programatically using j2me.Please give me idea to get this.What is the root folder in mobile to place the text file for accessible from j2me Application environment.
Saravanan.P
You need javax.microedition.io.file.FileConnection
Get root folder:
try {
Enumeration roots = FileSystemRegistry.listRoots();
while(roots.hasMoreElements()) {
System.out.println("Root: file:///"+(String)roots.nextElement());
}
} catch(Exception e) {
}
write to file
public void write(String root) {
FileConnection fc = null;
String fName = "test.txt";
try {
fc = (FileConnection) Connector.open(root + fName, Connector.READ_WRITE);
if(!fc.exists()) {
fc.create();
}
DataOutputStream dos = fc.openDataOutputStream();
dos.writeUTF("test-test");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fc.close();
} catch (IOException e) { }
}
}
read from file
public void read(String root) {
FileConnection fc = null;
try {
fc= (FileConnection) Connector.open(root + "test.txt", Connector.READ);
DataInputStream dis = fc.openDataInputStream();
String data = dis.readUTF();
System.out.println(data);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fc.close();
} catch (IOException e) { }
}
}
Better u use FileConnection.
FileConnection fc=(FileConnection)Connector.ope(url);