1

I am having problems related to Tapestry on my final year project (Maven + Hibernate + Spring + Tapestry). I hope somebody could help on it. I generate an XML file (its content is my MySql DB data in a custom format I created) on my service layer (I tried it and it is propperly generated: it is working). I tested it from my Junit tests. The problem is that I am unable to get it working from view layer, using Tapestry.

I tried this but unsuccessfully I think that it is because file does not exist already: it is dinamically generated when user clicks on "Download XML" link.

Here you are my source code (user clicks on a link which points to this page). POJO for the page (xmlService.exportXml is the method from my service layer which creates the XML file):

public class DownloadAll {
    @Component
    private Form xmlDownloadForm;

    @Property
    private File xmlFile;

    @Property
    @SessionState(create=false)
    private UserSession userSession;

    @Inject
    private XmlService xmlService;

    public StreamResponse onSubmit() {
      xmlFile = xmlService.exportXml(userSession.getUserProfileId());
      String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
      InputStream input = DownloadAll.class.getResourceAsStream("exportedData-"
          + userSession.getLoginName() + timeStamp + ".xml");
      return new XMLAttachment(input);
    }
}

And this is the page template:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_3.xsd"
    t:type="Layout" t:pageTitle="title"
    xmlns:p="tapestry:parameter"
    t:menuExplanation="menuExplanation">
      <form t:type="Form" t:id="xmlDownloadForm">
    <input type="submit" value="${message:download}"/>
  </form>
</html>

Does anybody know how to make it working? Thanks and regards.

Edit: File is generated (I can see it in the folder) when I submit the form but file is not served. I get this error instead:

org.apache.tapestry5.runtime.ComponentEventException Class es.udc.decompras.web.pages.xml.util.XMLAttachment has been transformed and may not be directly instantiated.

XMLAttachment is the same than JPEGAttachment.java from this link Here you are the source code:

public class XMLAttachment extends AttachmentStreamResponse {

    public XMLAttachment(InputStream is, String args) {
      super(is, args);
      this.contentType = "application/xml";
      this.extension = "xml";
    }

    public XMLAttachment(InputStream is) {
      super(is);
      this.contentType = "application/xml";
      this.extension = "xml";
    }
}
Alberto
  • 139
  • 1
  • 3
  • 14

1 Answers1

1

Only pages can be in your "pages" package. Move XMLAttachment class to any package not managed by tapestry (eg NOT base, components or pages).

Tapestry performs byte code magic on the managed packages and uses a special classloader to load them which is not compatible for utility classes etc.

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Thanks for the repply @uklance. I have XMLAttachment on an util subpackage (es.udc.decompras.web.pages.xml.util). Isn't the subpackage valid either? – Alberto Jun 10 '13 at 19:41
  • No, all sub packages are also managed. For pages, the subpackage will be part of the URL. – lance-java Jun 10 '13 at 19:45
  • Thank you very much @uklance! It really solved my issue :). I read that (from my Google searches) but I understood that this Java class shouldn't be on pages package: I didn't understood that this Java class shouldn't be on pages subpackage. – Alberto Jun 10 '13 at 20:06
  • he only problem I have now is that I am unable to serve the file using _InputStream input = DownloadAll.class.getResourceAsStream("exportedData-" + userSession.getLoginName() + timeStamp + ".xml")_ but it works perfectly if I use _InputStream input = new FileInputStream (xmlFile)_ instead :/ – Alberto Jun 10 '13 at 22:48
  • Try SomeNonComponentClass.class.getClassLoader().getResourceAsStream(...) as Class.getResourceAsStream(...) requires the resource to be in the same package folder as the class. This is not tapestry related. – lance-java Jun 11 '13 at 05:55
  • Thanks again @uklance but I create the file on xml/exported/ folder, which is on my project root folder (so it is in the same folder than my pom.xml file, the src folder and the target folder). Even if I try to get it using the class which generates the file I don't get it: `InputStream input = XmlService.class.getResourceAsStream(fullFilenamePath);` where fullFilenamePath is _"xml/exported" + filename_ . The same with fullFilename instead of fullFilenamePath. If I print `XmlService.class.getResource(fullFilename)` or if I print `XmlService.class.getResource(fullFilenamePath)` I get null. – Alberto Jun 11 '13 at 08:07
  • This is now getting totally off topic. If you are using maven, your file should go into src/main/resources if you want it to be included in your jar / war etc. As I said, you should be using `XmlService.class.getClassloader().getResource(fullFilenamePath)`. Try looking inside your jar file with winzip etc to see if the file is included. – lance-java Jun 11 '13 at 10:17