I'm writing a RCP/RAP-App with rap 1.4 as target platform for the rap part. In rcp I have a save button configured via menuContribution in the plugin.xml and an according SaveHandler for the save command. Now I like to use this button as a download button in my rap app.
Asked
Active
Viewed 1,110 times
1 Answers
2
I've got it.
I wrote a DownloadServiceHandler and created an unvisible Browser with the download-URL in my handler of the save-command.
All the work step by step:
The save-button in the toolbar is configured in the plugin.xml:
<extension
point="org.eclipse.ui.commands">
<command
id="pgui.rcp.command.save"
name="Save">
</command>
</extension>
<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="toolbar:org.eclipse.ui.main.toolbar">
<toolbar
id="pgui.rcp.toolbar1">
<command
commandId="pgui.rcp.command.save"
icon="icons/filesave_16.png"
id="pgui.rcp.button.save"
style="push"
</command>
</toolbar>
</menuContribution>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
class="pgui.handler.SaveHandler"
commandId="pgui.rcp.command.save">
</handler>
</extension>
I created a DownloadServiceHandler:
public class DownloadServiceHandler implements IServiceHandler
{
public void service() throws IOException, ServletException
{
final String fileName = RWT.getRequest().getParameter("filename");
final byte[] download = getYourFileContent().getBytes();
// Send the file in the response
final HttpServletResponse response = RWT.getResponse();
response.setContentType("application/octet-stream");
response.setContentLength(download.length);
final String contentDisposition = "attachment; filename=\"" + fileName + "\"";
response.setHeader("Content-Disposition", contentDisposition);
response.getOutputStream().write(download);
}
}
In the method postWindowCreate of the ApplicationWorkbenchWindowAdvisor I registered the DownloadServiceHandler:
private void registerDownloadHandler()
{
final IServiceManager manager = RWT.getServiceManager();
final IServiceHandler handler = new DownloadServiceHandler();
manager.registerServiceHandler("downloadServiceHandler", handler);
}
In the execute-method of the SaveHandler I created an unvisible Browser and set the url with the filename and the registered DownloadServiceHandler.
final Browser browser = new Browser(shell, SWT.NONE);
browser.setSize(0, 0);
browser.setUrl(createDownloadUrl(fileName));
.
.
private String createDownloadUrl(final String fileName)
{
final StringBuilder url = new StringBuilder();
url.append(RWT.getRequest().getContextPath());
url.append(RWT.getRequest().getServletPath());
url.append("?");
url.append(IServiceHandler.REQUEST_PARAM);
url.append("=downloadServiceHandler");
url.append("&filename=");
url.append(fileName);
return RWT.getResponse().encodeURL(url.toString());
}

Michael K.
- 1,738
- 2
- 17
- 35