spv03 / full-hibernate-plugin-for-struts2

Automatically exported from code.google.com/p/full-hibernate-plugin-for-struts2
0 stars 0 forks source link

java.lang.IllegalStateException thrown for struts2 <result type="stream"> #11

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. in struts.xml file let your package extend "hibernate-default"
2. add an action tag with an action class mapped.
3. add a result tag with type set to "stream"

What is the expected output? What do you see instead?
Expected:
  Once a request for this action comes in the file is streamed back as 
response with out any exceptions thrown.
Actual:
  File streams correctly, but finally throws an exception on the server 
console. java.lang.IllegalStateException.
I believe the hibernate plug-in is trying to write something to an already 
committed output stream causing the exception.

What version of the product are you using? On what operating system?
struts2-fullhibernatecore-plugin-2.1.2-GA
struts-2.1.8.1
OS: Windows Vista SP2
Java: 1.6.0_16

Please provide any additional information below.
Easily replicable using the struts provided examples (showcase) for file 
download.
Just change the extends attribute of the package tag from "struts-default" 
to "hibernate-default".

Original issue reported on code.google.com by amanimra...@gmail.com on 14 Jan 2010 at 1:29

Attachments:

GoogleCodeExporter commented 8 years ago
Thanks for this report. But, could you send me the Action code and the XML 
mappging file?

Original comment by jyoshiriro on 15 Jan 2010 at 2:14

GoogleCodeExporter commented 8 years ago
I made a simple example using Convention Plugin and I had No problem.

Model class:

@Entity
public class ImageTest implements Serializable{

    @Id
    @GeneratedValue(strategy = IDENTITY)
    int idimage;

    @Column
    byte[] content;

    public ImageTest() {
        // TODO Auto-generated constructor stub
    }

    public ImageTest(int idimage, byte[] content) {
        super();
        this.idimage = idimage;
        this.content = content;
    }

    public int getIdimage() {
        return idimage;
    }

    public void setIdimage(int idimage) {
        this.idimage = idimage;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }

}

Action:

@ParentPackage("hibernate-default")
public class VerImagemAction extends ActionSupport{

    @SessionTarget
    Session session;

    InputStream inputStream;

    @Action(
        results=@Result(type="stream")
    )
    public String execute() throws Exception {
        ImageTest imagem = (ImageTest) session.get(ImageTest.class, 1);
        inputStream = new ByteArrayInputStream(imagem.getContent());
        return super.execute();
    }

    public InputStream getInputStream() {
        return inputStream;
    }
}

hibernate.cfg.xml

<hibernate-configuration>
    <session-factory>
        <property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/ufpa</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <mapping class="modelo.ImageTest" />
    </session-factory>
</hibernate-configuration>

No struts.xml 

The Stream was successull generated and downloaded with no exception.

Hibernate Core 3.3.2
Hibernate Annotations 3.4
Hibernate Validator 3.1
MySql 5.1
Struts 2.1.8
FHP 2.1.2

Original comment by jyoshiriro on 15 Jan 2010 at 2:47

GoogleCodeExporter commented 8 years ago
Hi jyoshiriro,
I've tried to detail it as much as possible, if you need something please do 
let me 
know.
Thanks.

Struts.xml :
----------

<package name="downloadTest" namespace="/downloadTest" 
extends="hibernate-default">
    <action name="DownloadFileActionTest"
        class="xyz.DownloadFileAction">
        <interceptor-ref name="defaultStackHibernateStrutsValidation" />
        <result name="success" type="stream">
            <param name="contentType">application/pdf</param>
            <param name="inputName">inputStream</param>
            <param 
name="contentDisposition">filename="document.pdf"</param>
            <param name="bufferSize">1024</param>
        </result>
    </action>
</package>

Model/Entity, DocumentTempImpl :
-------------------------------

public class DocumentTempImpl {

    private Long objectId;
    private String documentCode;
    private byte[] data;

    public DocumentTempImpl() {
        super();
    }

    public DocumentTempImpl(Long objectId) {
        super(objectId);
    }

    public Long getObjectId() {
        return objectId;
    }

    public void setObjectId(Long objectId) {
        this.objectId = objectId;
    }
    public String getDocumentCode() {
        return documentCode;
    }

    public void setDocumentCode(String documentCode) {
        this.documentCode = documentCode;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

}

Action, DownloadFileAction.java :
--------------------------------

import xyz.Document;
import xyz.DocumentTempImpl;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.opensymphony.xwork2.ActionSupport;

/*
 * Attempts to fetch a pdf file and send to user.
 * The pdf file resides in DB, Fetched using hibernate.
 *  
 */
public class DownloadFileAction extends ActionSupport {

    private String documentId = "98";
    private InputStream inputStream;

    @SessionTarget
    Session hSession;

    @TransactionTarget
    Transaction hTransaction;

    public String execute() throws Exception {
        fetch();
        return SUCCESS;
    }

    private void fetch() throws Exception {
        DocumentTempImpl doc = (DocumentTempImpl)find(DocumentTempImpl.class,
                Long.valueOf(documentId));

        byte[] data = doc.getData();
        inputStream = new ByteArrayInputStream(data);

    }

    public InputStream getInputStream() {
        return inputStream;
    }

    public Object find(Class clazz, Long id) {
        Query q = hSession.createQuery("from " + clazz.getName()
                + " as t where t.id =:longVal");
        q.setLong("longVal", id);

        return q.uniqueResult();
    }

}

Hibernate Configuration, hibernate.cfg.xml :
-------------------------------------------

<hibernate-configuration>
    <session-factory>
        <property name="connection.useUnicode">true</property>
        <property name="connection.characterEncoding">UTF-8</property>
        <property 
name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property 
name="connection.url">jdbc:mysql://localhost:3306/test</property>
        <property name="connection.username">test</property>
        <property name="connection.password">test</property>
        <property 
name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="current_session_context_class">thread</property>
        <property name="hibernate.transaction.factory_class">
            org.hibernate.transaction.JDBCTransactionFactory</property>

        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="use_sql_comments">true</property>

        <mapping resource="xyz/DocumentTempImpl.hbm.xml" />

    </session-factory>
</hibernate-configuration>

Hibernate Mapping, DocumentTempImpl.hbm.xml :
-------------------------------------------

<hibernate-mapping>

    <class name="xyz.DocumentTempImpl" table="DOCUMENT_TEMP">
        <id name="objectId" column="DOCUMENT_TEMP_ID" access="field">
            <generator class="native" />
        </id>
        <property name="documentCode" column="CODE" type="string"
            access="field" />
        <property name="data" access="field">
            <column name="FILE" sql-type="MEDIUMBLOB" />
        </property>
    </class>
</hibernate-mapping>

Hibernate Core 3.3.2 GA
Hibernate Annotations 3.4
Hibernate Validator 3.1
MySql 5.1.41
Struts 2.1.8
FHP 2.1.2 GA
JRE/JDK 1.6

Original comment by amanimra...@gmail.com on 15 Jan 2010 at 7:40

Attachments:

GoogleCodeExporter commented 8 years ago
If this is useful.... apache-tomcat-5.5.28

Original comment by amanimra...@gmail.com on 15 Jan 2010 at 11:54

GoogleCodeExporter commented 8 years ago
I used your file in a new project using Apache Derby Embedded Database and the 
data
was downloaded successful. 

Is the content of you colunm "data" a valid PDF file?

Original comment by jyoshiriro on 15 Jan 2010 at 12:52

GoogleCodeExporter commented 8 years ago
I tried now with mysql 5.1 with success too. But I use Tomcat 6.x.

So, the problem can be the Tomcat version or the content of your table.

Original comment by jyoshiriro on 15 Jan 2010 at 1:10

GoogleCodeExporter commented 8 years ago
Yes it was tomcat-5.5.28, that was causing the problem. I changed to 
tomcat-6.0.20 and 
the exception stopped showing up.

Thanks a lot.

Original comment by amanimra...@gmail.com on 16 Jan 2010 at 6:12