package org.osiam.addons.administration.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * This class is responsible for creating an redirect string that can be returned by Spring-Request-Handler.
 */
public class RedirectBuilder {
    private String destination;
    private String path;
    private String query;
    private Map<String, Object> parameters = new HashMap<String, Object>();

    /**
     * Set the url-path of the redirect destination.
     * 
     * @param path
     *        The url path.
     * @return this
     */
    public RedirectBuilder setPath(String path) {
        this.path = path;

        return this;
    }

    /**
     * Set the url-query of the redirect destination. 
     * This query should be encoded before!
     * 
     * @param query
     *        The query of the url.
     * @return this
     */
    public RedirectBuilder setQuery(String query) {
        this.query = query;

        return this;
    }

    /**
     * Set the complete redirect destination.
     * 
     * @param destination
     *        The <b>complete</b> destination.
     * @return this
     */
    public RedirectBuilder setDestination(String destination) {
        this.destination = destination;

        return this;
    }

    /**
     * Add a parameter which will be append at the url query.
     * The value of the parameter will be encode automatically.
     * 
     * @param key
     *        The name of the parameter.
     * @param value
     *        The value of the parameter.
     * @return this
     */
    public RedirectBuilder addParameter(String key, Object value) {
        this.parameters.put(key, value);

        return this;
    }

    /**
     * Return the redirect string. That can be used for spring-request-handler.
     * 
     * @return The redirect string.
     */
    public String build() {
        StringBuilder redirect = new StringBuilder("redirect:");

        if (destination != null) {
            redirect.append(destination);
        } else {
            if (path != null) {
                redirect.append(path);
            }

            if (query != null) {
                redirect.append("?");
                redirect.append(query);
            }

            if (!parameters.isEmpty()) {
                if (query == null) {
                    redirect.append("?");
                } else {
                    redirect.append("&");
                }

                boolean isFirst = true;
                for (Entry<String, Object> param : parameters.entrySet()) {
                    if (!isFirst) {
                        redirect.append("&");
                    }

                    redirect.append(param.getKey());
                    if (param.getValue() != null) {
                        redirect.append("=");

                        try {
                            String encodedValue = URLEncoder.encode(param.getValue().toString(), "UTF-8");
                            redirect.append(encodedValue);
                        } catch (UnsupportedEncodingException e) {
                            throw new IllegalStateException(e);
                        }
                    }

                    isFirst = false;
                }
            }
        }

        return redirect.toString();
    }
}

@@ -44,81 +44,8 @@
 l;%0A%0A
-import java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0A
 impo
@@ -117,16 +117,53 @@
 Entry;%0A%0A
+import javax.ws.rs.core.UriBuilder;%0A%0A
 /**%0A * T
@@ -773,24 +773,16 @@
 nation. 
-%0A     * 
 This que
@@ -1376,23 +1376,16 @@
 l query.
-%0A     *
  The val
@@ -1909,22 +1909,19 @@
     
-String
+Uri
 Builder 
 redi
@@ -1920,49 +1920,18 @@
 der 
-redirect = new StringBuilder(%22redirect:%22)
+uri = null
 ;%0A%0A 
@@ -1976,31 +1976,40 @@
         
-redirect.append
+uri = UriBuilder.fromUri
 (destina
@@ -2024,32 +2024,76 @@
        %7D else %7B%0A
+            uri = UriBuilder.fromPath(%22%22);%0A%0A
             if (
@@ -2124,31 +2124,24 @@
         
-redirect.append
+uri.path
 (path);%0A
@@ -2208,61 +2208,24 @@
     
-redirect.append(%22?%22);%0A                redirect.append
+uri.replaceQuery
 (que
@@ -2305,941 +2305,168 @@
     
-if (query == null) %7B%0A                    redirect.append(%22?%22);%0A                %7D else %7B%0A                    redirect.append(%22&%22);%0A                %7D%0A%0A                boolean isFirst = true;%0A                for (Entry%3CString, Object%3E param : parameters.entrySet()) %7B%0A                    if (!isFirst) %7B%0A                        redirect.append(%22&%22);%0A                    %7D%0A%0A                    redirect.append(param.getKey());%0A                    if (param.getValue() != null) %7B%0A                        redirect.append(%22=%22);%0A%0A                        try %7B%0A                            String encodedValue = URLEncoder.encode(param.getValue().toString(), %22UTF-8%22);%0A                            redirect.append(encodedValue);%0A                        %7D catch (UnsupportedEncodingException e) %7B%0A                            throw new IllegalStateException(e);%0A                        %7D%0A                    %7D%0A%0A                    isFirst = false
+for (Entry%3CString, Object%3E param : parameters.entrySet()) %7B%0A                    uri.queryParam(param.getKey(), param.getValue() != null ? param.getValue() : %22%22)
 ;%0A  
@@ -2521,24 +2521,25 @@
  return 
+%22
 redirect
 .toStrin
@@ -2530,16 +2530,24 @@
 redirect
+:%22 + uri
 .toStrin

use URIBuilder instead of using string concatenation