Coverage Report - javax.faces.internal.WindowIdUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
WindowIdUtil
90%
19/21
100%
8/8
1.429
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 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,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.internal;
 17  
 
 18  
 import java.util.Map;
 19  
 import java.util.Random;
 20  
 
 21  
 import javax.faces.FacesException;
 22  
 import javax.faces.context.ExternalContext;
 23  
 
 24  
 import org.seasar.framework.util.StringUtil;
 25  
 import org.seasar.teeda.core.JsfConstants;
 26  
 
 27  
 /**
 28  
  * @author higa
 29  
  *
 30  
  */
 31  
 public abstract class WindowIdUtil {
 32  
 
 33  
     public static final String NEWWINDOW = "newwindow";
 34  
 
 35  
     public static final String WID = "wid";
 36  
 
 37  
     private static final String BLANK = "_blank";
 38  
 
 39  1
     private static Random random = new Random(System.currentTimeMillis());
 40  
 
 41  0
     protected WindowIdUtil() {
 42  0
     }
 43  
 
 44  
     public static String setupWindowId(final ExternalContext externalContext)
 45  
             throws FacesException {
 46  3
         String wid = null;
 47  3
         if (needNewWindow(externalContext.getRequestParameterMap())) {
 48  1
             wid = createWindowId();
 49  
         } else {
 50  2
             Map paramMap = externalContext.getRequestParameterMap();
 51  2
             wid = (String) paramMap.get(WID);
 52  
         }
 53  3
         setWindowId(externalContext, wid);
 54  3
         return wid;
 55  
     }
 56  
 
 57  
     public static synchronized String createWindowId() {
 58  3
         return Long.toString(random.nextLong());
 59  
     }
 60  
 
 61  
     public static String getWindowId(final ExternalContext externalContext)
 62  
             throws FacesException {
 63  30
         Map requestMap = externalContext.getRequestMap();
 64  30
         return (String) requestMap.get(WindowIdUtil.WID);
 65  
     }
 66  
 
 67  
     public static void setWindowId(final ExternalContext externalContext,
 68  
             String wid) throws FacesException {
 69  7
         Map requestMap = externalContext.getRequestMap();
 70  7
         requestMap.put(WID, wid);
 71  7
     }
 72  
 
 73  
     public static boolean isNewWindowTarget(String target) {
 74  33
         if (StringUtil.isEmpty(target)) {
 75  26
             return false;
 76  
         }
 77  7
         return BLANK.equals(target) || target.charAt(0) != '_';
 78  
     }
 79  
 
 80  
     public static boolean needNewWindow(final Map parameterMap) {
 81  6
         final String newwindow = (String) parameterMap.get(NEWWINDOW);
 82  6
         return JsfConstants.TRUE.equals(newwindow);
 83  
     }
 84  
 }