Groovy: org.apache.bsf.BSFException: exception from Groovy: groovy.lang.MissingPropertyException: No

Hi 

I am a newbie to Groovy and Java. 

I have below Groovy code to hit an URL that returns the output in json format and convert to XML file. 

When I use the $top and $skip parameters, (these parameters have to be used to fetch the next page of data) this code throws the below error. 

org.apache.bsf.BSFException: exception from Groovy: groovy.lang.MissingPropertyException: No such property: top for class:


Please help to resolve the issue.

 

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Properties;
import java.util.zip.*;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.events.XMLEvent;

// 
// Uses STAXON to convert JSON to XML
//
import de.odysseus.staxon.json.JsonXMLConfig;
import de.odysseus.staxon.json.JsonXMLConfigBuilder;
import de.odysseus.staxon.json.JsonXMLInputFactory;
import de.odysseus.staxon.xml.util.PrettyXMLEventWriter;

//
// Base64 Authentication is added for WA Requirement to access MYOB API
//
import org.apache.commons.codec.binary.Base64;

public class ODIFromJSON {
public static void main(String[] args) throws Exception {
if (args.length < 4) {
System.err.println("Invalid args.");
return;
}
if (args.length == 5) {
process(args[0], args[1], args[2], args[3], args[4]);
} else {
process(args[0], args[1], args[2], args[3]);
}
}
public static void process(String inp, String outp, String param1, String param2) throws Exception {
process(inp, outp, param1, param2, null);
}

public static void process(String inp, String outp, String param1, String param2, String param3) throws Exception {
String myobCredential = "User:password";
String myobCFURL = "http://localhost:8080/AccountRight/c2716558-5beb-421c-995e-dcf88d68781d";

byte[] authEncBytes = Base64.encodeBase64(myobCredential.getBytes());
String authStringEnc = new String(authEncBytes);

URL url = new URL(inp);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
InputStream input = urlConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(input);

//InputStream input = new URL(inp).openStream();
Properties props = new Properties();
if (param1 != null && param1 != "")
props.put("param1",param1);
if (param2 != null && param2 != "")
props.put("param2",param2);

if (param3 != null) props.put("param3",param3);
InputStream is = new ODIFromJSON().getInputStream(input, props); 
int size = 0;
byte[] buffer = new byte[1024];
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outp, true));
while ((size = is.read(buffer)) != -1) { bos.write(buffer, 0, size); }
bos.flush(); bos.close();

}


public InputStream getInputStream(InputStream input, Properties properties) throws Exception {

PipedInputStream inp = null;

String param3 = properties.getProperty("param3");

if (param3 != null) {
PipedOutputStream outputz = new PipedOutputStream();
PipedInputStream inputz = new PipedInputStream(outputz);
new UnzipThread(input, outputz, properties).start();

PipedOutputStream output = new PipedOutputStream();
inp = new PipedInputStream(output);
new TransformThread(inputz, output, properties).start();
} else {
PipedOutputStream output = new PipedOutputStream();
inp = new PipedInputStream(output);
new TransformThread(input, output, properties).start();
}

return inp;
}


class UnzipThread extends Thread implements Runnable {

InputStream input = null;
OutputStream output = null; 
Properties properties = null;
final int BUFFER = 2048;

UnzipThread( InputStream pi, OutputStream po, Properties pps) {
this.input = pi; this.output = po; properties = pps; 
}

public void run() {
try {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(input));
ZipEntry entry;
BufferedOutputStream dest = new BufferedOutputStream(output, BUFFER);
while((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " +entry);
int count;
byte[] data = new byte[BUFFER];
//def data = [] as byte[BUFFER];

while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
}
dest.flush();
dest.close();
zis.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}

class TransformThread extends Thread implements Runnable {

InputStream input = null;
OutputStream output = null; 
Properties properties = null;

TransformThread( InputStream pi, OutputStream po, Properties pps) {
this.input = pi; this.output = po; properties = pps; 
}

public void run() {
XMLEventFactory factory = XMLEventFactory.newInstance();
JsonXMLConfig config = new JsonXMLConfigBuilder().multiplePI(false).build();
try {
JsonXMLInputFactory inputFactory = new JsonXMLInputFactory(config);
String param1 = properties.getProperty("param1");
String param2 = properties.getProperty("param2");
if (param2 != null)
inputFactory.setProperty(JsonXMLInputFactory.PROP_VIRTUAL_ROOT, param2);
XMLEventReader reader = inputFactory.createXMLEventReader(input);
XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(output);

if (param1 != null) {
writer.add(factory.createStartDocument());
writer.add(factory.createStartElement("", "", param1));
while (reader.hasNext()) {
XMLEvent event = reader.nextEvent();
if (!event.isStartDocument() && !event.isEndDocument()) {
writer.add(event);
}
}
writer.add(factory.createEndElement("", "", param1));
writer.add(factory.createEndDocument());
}

writer = new PrettyXMLEventWriter(writer); // format output
writer.add(reader);
reader.close();
writer.close();
} catch (Exception e) {e.printStackTrace(); }
finally {
try {
output.close();
input.close();
} catch (Exception e) {e.printStackTrace(); }
}
}
}

}

ODIFromJSON.process("http://localhost:8080/AccountRight/c2716558-5beb-421c-995e-dcf88d68781d/Inventory/Item/?api-version=v2&format=json", "D:\\etl\\toptest.xml", "INVENTORY", "PRODUCT");

1 answer

0 votes
David Maye Atlassian Team Jan 20, 2017

Hi,

I don't think you're in the right place, as I don't see how this relates to Atlassian products or HipChat (which was tagged). You may have better luck asking the Groovy community at: http://www.groovy-lang.org/community.html or doing a Google search of the error specifically.

Hope this helps!

Cheers,
-David
HipChat Server Support Engineer | Atlassian

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 10, 2018 in Hipchat

What should I think about when migrating HipChat to Slack?

...from the beginning. We have built up a lot of content in HipChat, with it being a core tool in our distributed company model. While it is true that we didn’t need to move to Slack immediately, we felt it...

476 views 1 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you