|
| 1 | +/* |
| 2 | + * Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.ocpsoft.rewrite.faces.navigate; |
| 17 | + |
| 18 | +import java.io.UnsupportedEncodingException; |
| 19 | +import java.net.URLEncoder; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map.Entry; |
| 22 | + |
| 23 | +import org.ocpsoft.common.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * Helper class to build JSF action outcomes. |
| 27 | + * |
| 28 | + * @author Christian Kaltepoth |
| 29 | + */ |
| 30 | +public class Navigate |
| 31 | +{ |
| 32 | + |
| 33 | + private final String viewId; |
| 34 | + |
| 35 | + private final ParameterMap parameters = new ParameterMap(); |
| 36 | + |
| 37 | + private boolean redirect = true; |
| 38 | + |
| 39 | + private Navigate(String viewId) |
| 40 | + { |
| 41 | + this.viewId = viewId; |
| 42 | + } |
| 43 | + |
| 44 | + public static Navigate to(String viewId) |
| 45 | + { |
| 46 | + Assert.notNull(viewId, "viewId must not be null"); |
| 47 | + return new Navigate(viewId); |
| 48 | + } |
| 49 | + |
| 50 | + public Navigate with(CharSequence name, Object value) |
| 51 | + { |
| 52 | + Assert.notNull(name, "name must not be null"); |
| 53 | + if (value != null) { |
| 54 | + parameters.put(name.toString(), value.toString()); |
| 55 | + } |
| 56 | + return this; |
| 57 | + } |
| 58 | + |
| 59 | + public Navigate withoutRedirect() |
| 60 | + { |
| 61 | + redirect = false; |
| 62 | + return this; |
| 63 | + } |
| 64 | + |
| 65 | + public String build() |
| 66 | + { |
| 67 | + |
| 68 | + ParameterMap query = parameters.copy(); |
| 69 | + |
| 70 | + if (redirect) { |
| 71 | + query.put("faces-redirect", "true"); |
| 72 | + } |
| 73 | + |
| 74 | + StringBuilder outcome = new StringBuilder(); |
| 75 | + outcome.append(viewId); |
| 76 | + |
| 77 | + boolean first = true; |
| 78 | + for (Entry<String, List<String>> param : query.entrySet()) { |
| 79 | + |
| 80 | + for (String value : param.getValue()) { |
| 81 | + |
| 82 | + outcome.append(first ? '?' : '&'); |
| 83 | + |
| 84 | + outcome.append(encodeQuery(param.getKey())); |
| 85 | + outcome.append('='); |
| 86 | + outcome.append(encodeQuery(value)); |
| 87 | + |
| 88 | + first = false; |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + return outcome.toString(); |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private static String encodeQuery(String str) |
| 99 | + { |
| 100 | + try { |
| 101 | + return URLEncoder.encode(str, "UTF-8"); |
| 102 | + } |
| 103 | + catch (UnsupportedEncodingException e) { |
| 104 | + throw new IllegalArgumentException(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public String toString() |
| 110 | + { |
| 111 | + return build(); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments