newdev
newdev

Reputation: 59

MarkLogic: No stream to write

I'm having a problem where I have a method that gets parameters from the AngularJs front-end, creates an object with them, writes the object as XML file in a folder, and then is supposed to write that XML file into the MarkLogic database.

However, the part where it's supposed to write to the database appears to see as if the file doesn't exist, even though it does:

Here's the code:

@RequestMapping(value = "/add/korisnik", method = RequestMethod.POST)
public String addKorisnik(@RequestParam String ime, @RequestParam String prezime, @RequestParam String username, @RequestParam String password, @RequestParam String orcid, @RequestParam String role) throws JAXBException, FileNotFoundException{

    Korisnik.Roles roles = new Korisnik.Roles();
    roles.setRole(role);
    Korisnik k = new Korisnik();
    k.setIme(ime);
    k.setPrezime(prezime);
    k.setUsername(username);
    k.setPassword(password);
    k.setOrcid(orcid);
    k.setRoles(roles);

    System.out.println(k.toString());

    // create JAXB context and instantiate marshaller
    JAXBContext context = JAXBContext.newInstance(Korisnik.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    StringWriter sw = new StringWriter();
    m.marshal(k, sw);

    // Write to File
    File f = new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml");
    if (f.exists()) { 
        return "Username already taken.";
    }
    else {
        m.marshal(k, new File("src/main/resources/data/korisnici/" + k.getUsername() + ".xml"));
    }

    // acquire the content
    InputStream docStream = ObjavaNaucnihRadovaApplication.class.getClassLoader().getResourceAsStream(
                "data/korisnici/" + k.getUsername() + ".xml");

    // create the client
    DatabaseClient client = DatabaseClientFactory.newClient(MarkLogicConfig.host,
                MarkLogicConfig.port, MarkLogicConfig.admin,
                MarkLogicConfig.password, MarkLogicConfig.authType);

    // create a manager for XML documents
    XMLDocumentManager docMgr = client.newXMLDocumentManager();

    // create a handle on the content
    InputStreamHandle handle = new InputStreamHandle(docStream);

    // write the document content
    docMgr.write("http://localhost:8011/korisnici/" + k.getUsername()+".xml", handle);  

    //release the client
    client.release();

    return "OK";
}

Upvotes: 0

Views: 62

Answers (2)

DALDEI
DALDEI

Reputation: 3732

Several issues.

  • First, the file you write to is not the same as you read from. You are writing to "src/main/resources/data/korisnici/.." which is relative to the current directory of the JVM (application server). You are reading from the classpath resource directory -- not likely to be the same. You could simply reuse the same File object then they would be the same.

  • Second, you don't need to write to disk this small of an object, just write it to a in-memory stream (like ByteArrayStream() ).

Upvotes: 2

Mads Hansen
Mads Hansen

Reputation: 66781

It sounds as if the classloader isn't finding your file.

If you already have a File object that you have just written, why not construct an InputStream from it, rather than attempt to find it from a differently constructed relative path?

InputStream docStream = new FileInputStream(f);

Upvotes: 0

Related Questions