/*
 * Copyright 2016 ZJNU ACM.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.edu.zjnu.acm.judge.util;

import com.github.zhanhb.download.URLEncoder;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author zhanhb
 */
public class URLBuilder {

    /**
     * https://tools.ietf.org/html/rfc3986#section-3.4
     */
    private static final URLEncoder ENCODER = new URLEncoder("-._~!$'()*,;:@");

    public static URLBuilder fromRequest(HttpServletRequest request) {
        return new URLBuilder(request.getServletPath(), request.getParameterMap());
    }

    private String path;
    private final Map<String, String[]> query;

    private URLBuilder(String path, Map<String, String[]> query) {
        this.path = path;
        this.query = new LinkedHashMap<>(query);
    }

    public URLBuilder replaceQueryParam(String name, String... values) {
        if (values == null || values.length == 0) {
            query.remove(name);
        } else {
            query.put(name, values.clone());
        }
        return this;
    }

    public URLBuilder replacePath(String path) {
        this.path = path != null ? path : "";
        return this;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(encode(path));
        boolean first = true;
        for (Map.Entry<String, String[]> entry : query.entrySet()) {
            String key = encode(entry.getKey());
            String[] value = entry.getValue();
            if (value != null) {
                for (String string : value) {
                    if (first) {
                        first = false;
                        sb.append('?');
                    } else {
                        sb.append('&');
                    }
                    sb.append(key).append("=").append(encode(string));
                }
            }
        }
        return sb.toString();
    }

    private String encode(String string) {
        return ENCODER.encode(string);
    }

}

@@ -934,16 +934,22 @@
 Encoder 
+QUERY_
 ENCODER 
@@ -980,16 +980,104 @@
 '()*,;:@
+/?%22);%0A    private static final URLEncoder PATH_ENCODER = new URLEncoder(%22-._~!$'()*,;:@/
 %22);%0A%0A   
@@ -1927,16 +1927,29 @@
 Builder(
+PATH_ENCODER.
 encode(p
@@ -2079,16 +2079,30 @@
 g key = 
+QUERY_ENCODER.
 encode(e
@@ -2267,225 +2267,70 @@
     
-if (first) %7B%0A                        first = false;%0A                        sb.append('?');%0A                    %7D else %7B%0A                        sb.append('&');%0A                    %7D%0A                    sb.append
+sb.append(first ? '?' : '&').append(QUERY_ENCODER.encode
 (key)
+)
 .app
@@ -2345,16 +2345,30 @@
 .append(
+QUERY_ENCODER.
 encode(s
@@ -2396,24 +2396,35 @@
     
-%7D%0A
     
-  
+first = false;%0A
       
-%7D%0A
+  
     
@@ -2441,116 +2441,51 @@
     
-return sb.toString();%0A
     %7D%0A
-%0A
     
-private String encode(String string) %7B%0A        return ENCODER.encode(s
+    %7D%0A        return sb.toS
 tring
+(
 );%0A 

Encode the path and the query key