I need to redirect to an external URL from a Vaaidin app when clicking on a button but the the trick is that i need to add a header to the response e.g. testHeader,test. I’ve already tried getting the response from the Vaadin service and call a redirect but that didnt work. A requestDispatcher wont do the trick because the response is already commited. I was looking at the open(URL) method but it looks like there is no option to add headers.
do you want to redirect the browser window to the redirect url and the browser has to add a special header to his request?
I don’t think this is possible. As far as i know the only informations of an URL that are send to the server are the request path and the GET parameters (the parameters behind the first question mark of the URL).
You can not redirect that way. It will just redirect the current ajax request to the redirect url, not the browser window. This will cause a “Comunication problem” error. If your application uses @Push it might don’t have a VaadinService.getCurrentResponse() because it uses a web socket instead of http requests to communicate with the server. This will cause a NullPointerException.
I have found a very complicated way to achieve what I think you want to do
public class MyVaadinUI extends UI {
@Override
protected void init(VaadinRequest request) {
// Add a custom request handler
VaadinSession.getCurrent().addRequestHandler(
new RequestHandler() {
@Override
public boolean handleRequest(VaadinSession session,
VaadinRequest request, VaadinResponse response)
throws IOException {
if ("/redirect".equals(request.getPathInfo())) {
response.setStatus(307); // Temporary Redirect
response.setHeader("Location", "http://www.google.de"); // Redirect url
response.setHeader("testHeader", "header"); // Your test header
return true;
}
return false;
}
});
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
final Resource redirectResource =
new ExternalResource("vaadin://../redirect");
// Redirect to the custom request handler using a link
Link link = new Link("Redirect", redirectResource);
layout.addComponent(link);
// Redirect to the custom request handler using a button
Button button = new Button("Redirect", new Button.ClickListener() {
@Override
public void buttonClick(Button.ClickEvent event) {
// Deprecated!
Page.getCurrent().open(redirectResource, "_self", true);
}
});
layout.addComponent(button);
}
}
We got central app that contains allowed apps for the user in the system. You can launch app from “dashboard”, but the problem is because we need extra header (authorization token). This header should be attach when opening URL path to the app (local). Opened apps than get this header and validate it, so the user dont need to login again.
With this sample (i know its a bit old but still), it just redirect to somewhere (which has extra header) but it does not send it to the real URL for app. It makes two request 1) redirect, 2) real app url.
Any suggestions ? Maybe some newer solutions. Thanks in advance.
I’m in search for a solution to this, too. Se please let me know if you found one. In my case it’s even worse, because my approach used a BrowserFrame to embed the destination URL as an ExternalResource.
It works in our case because we encode the parameter. And on the other side (app which is called), just read the parameter from vaadin request and use them (for auto login and some other stuff).
I hope this helps someone in the future. Have a nice day.
Hi I implemented Max’s solution. It works nice, but in my case I don’t want to move the location so I’m ommiting the response.setHeader(“Location”… part.
I’m basically generating a report file and pushing it to the outputstream of the response. The effect is the user presses the button and his report shows as being downloaded by the browser.
The problem is that I lose connection with the application (I can see it, but can’t press anything such as menu tabs etc.) and I need to refresh the browers to gain access to the application again.