javaeclipsetomcatservletseclipse-rap

eclipse rap download service servlet error


Recently, I have made an Eclipse RAP program using javax.servlet package to create a File Download service. It works pretty well when using the built-in server inside the Eclipse IDE. However, after deploying the program on the Tomcat server, while opening the download link, the error message shows HTTP 500 error as following:

Type: Exception Report

Message: Servlet execution threw an exception Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception: javax.servlet.ServletException: Servlet execution threw an exception org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Root Cause: java.lang.LinkageError: loader constraint violation: loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) previously initiated loading for a different type with name "javax/servlet/http/HttpServletResponse" testproject.DownloadServiceHandler.service(DownloadServiceHandler.java:59) org.eclipse.rap.rwt.engine.RWTServlet.handleValidRequest(RWTServlet.java:135) org.eclipse.rap.rwt.engine.RWTServlet.handleRequest(RWTServlet.java:117) org.eclipse.rap.rwt.engine.RWTServlet.doGet(RWTServlet.java:100) javax.servlet.http.HttpServlet.service(HttpServlet.java:635) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.eclipse.rap.rwt.osgi.internal.CutOffContextPathWrapper.service(CutOffContextPathWrapper.java:106) org.eclipse.equinox.http.servlet.internal.HttpServiceRuntimeImpl$LegacyServlet.service(HttpServiceRuntimeImpl.java:1240) org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:151) org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:65) org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:134) org.eclipse.equinox.http.servlet.internal.servlet.ProxyServlet.service(ProxyServlet.java:103) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.eclipse.equinox.servletbridge.BridgeServlet.service(BridgeServlet.java:158) javax.servlet.http.HttpServlet.service(HttpServlet.java:742) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Following is my code and related file:

BasicApplication.java

package testproject;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.rap.rwt.application.Application;
import org.eclipse.rap.rwt.application.ApplicationConfiguration;
import org.eclipse.rap.rwt.client.WebClient;

public class BasicApplication implements ApplicationConfiguration {

    public void configure(Application application) {
        Map<String, String> properties = new HashMap<String, String>();
        properties.put(WebClient.PAGE_TITLE, "Test Project");
        application.addEntryPoint("/", BasicEntryPoint.class, properties);
    }

}

BasicEntryPoint.java

package testproject;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files; 
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.application.AbstractEntryPoint;
import org.eclipse.rap.rwt.client.service.UrlLauncher;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;


public class BasicEntryPoint extends AbstractEntryPoint {

    @Override
    protected void createContents(Composite parent) {
        parent.setLayout(new GridLayout(2, false));

        Button downloadButton = new Button(parent, SWT.PUSH);
        downloadButton.setText("Download");
        downloadButton.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent e) {
              try {
                    File testfile = new File("/home/user/Desktop/test.file");
                    byte[] fileContent = Files.readAllBytes(testfile.toPath());
                    sendDownload(fileContent, testfile.getName());
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
              }
            });
      }

    public boolean sendDownload(byte[] data, String filename) {
        DownloadServiceHandler service = new DownloadServiceHandler(data, filename);
        service.register();
        UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
        launcher.openURL(service.getURL());
        return true;
    }

}

DownloadServiceHandler.java

package testproject;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.service.ServiceHandler;

public class DownloadServiceHandler implements ServiceHandler {

    private final byte[] data;
    private final String filename;
    private String id;

    public DownloadServiceHandler(byte[] data, String filename) {
        this.data = data;
        this.filename = filename;
        this.id = calculateId();
    }

    public String getURL() {
        return RWT.getServiceManager().getServiceHandlerUrl(getId());
    }

    private String getId() {
        return id;
    }

    private String calculateId() {
        return String.valueOf(System.currentTimeMillis()) + data.length;
    }

    public boolean register() {
        try {
            RWT.getServiceManager().registerServiceHandler(getId(), this);
            return true;
        } catch (Exception e) {
            System.out.println("failed to register download service handler");
            return false;
        }
    }

    private boolean unregister() {
        try {
            RWT.getServiceManager().unregisterServiceHandler(getId());
            return true;
        } catch (Exception e) {
            System.out.println("failed to unregister download service handler");
            return false;
        }
    }

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            response.setContentType("application/octet-stream"); // this is the error line "DownloadServiceHandler.java:59"
            response.setContentLength(data.length);
            response.setHeader("Content-Disposition", "attachment; filename=\"" + filename
                    + "\"");
            response.getOutputStream().write(data);
        } catch (Exception e) {
            System.out.println("failed to dispatch download");
        } finally {
            unregister();
        }
    }

}

META-INF/MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Testproject
Bundle-SymbolicName: testproject
Bundle-Version: 1.0.0.qualifier
Automatic-Module-Name: testproject
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.rap.rwt;bundle-version="[3.0.0,4.0.0)"
Service-Component: OSGI-INF/contribution.xml
Import-Package: javax.servlet;version="3.1.0",
 javax.servlet.http;version="3.1.0"
Bundle-ClassPath: .

OSGI-INF/contribution.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="BasicApplication">
   <implementation class="testproject.BasicApplication"/>
   <service>
      <provide interface="org.eclipse.rap.rwt.application.ApplicationConfiguration"/>
   </service>
</scr:component>

How am I suppose to fix this bug? I do not know how come it works in Eclipse IDE but not working after deploying it on the Tomcat server. Thanks in advance.


Solution

  • I have found the solution by myself....

    I restricted my project by only using javax.servlet version 3.1.0. Maybe the version of Tomcat Server which I was installed is not support. I am not sure about this but I revised the following part in META-INF/MANIFEST.MF and the problem was fixed.

    Import-Package: javax.servlet;version="[2.3.0,4.0.0)", javax.servlet.http;version="[2.3.0,4.0.0)"