Coverage Report - org.seasar.teeda.core.render.html.support.PortletUrlBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletUrlBuilder
0%
0/51
0%
0/34
11
 
 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 org.seasar.teeda.core.render.html.support;
 17  
 
 18  
 import java.net.URI;
 19  
 import java.util.ArrayList;
 20  
 import java.util.Iterator;
 21  
 import java.util.Map;
 22  
 
 23  
 import javax.faces.context.ExternalContext;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.portlet.PortletURL;
 26  
 import javax.portlet.RenderResponse;
 27  
 
 28  
 import org.seasar.teeda.core.portlet.FacesPortlet;
 29  
 
 30  0
 public class PortletUrlBuilder extends UrlBuilder {
 31  
     public String build() {
 32  0
         URI uri = URI.create(getBase());
 33  0
         if (uri.getScheme() != null) {
 34  0
             return super.build();
 35  
         }
 36  0
         FacesContext context = FacesContext.getCurrentInstance();
 37  0
         if (context != null) {
 38  0
             String viewId = calculatePath(context.getViewRoot().getViewId(),
 39  
                     uri.getPath());
 40  0
             ExternalContext externalContext = context.getExternalContext();
 41  0
             if (externalContext.getResponse() instanceof RenderResponse) {
 42  0
                 RenderResponse response = (RenderResponse) externalContext
 43  
                         .getResponse();
 44  
                 // uses ActionURL 
 45  0
                 PortletURL portletUrl = response.createActionURL();
 46  0
                 portletUrl.setParameter(FacesPortlet.VIEW_ID, viewId);
 47  0
                 portletUrl.setParameter(FacesPortlet.RENDER_PARAMETER, "true");
 48  
 
 49  0
                 if (uri.getQuery() != null) {
 50  0
                     String[] queries = uri.getQuery().split("&");
 51  0
                     for (int i = 0; i < queries.length; i++) {
 52  0
                         int index = queries[i].indexOf("=");
 53  0
                         if (index > 0) {
 54  0
                             portletUrl.setParameter(queries[i].substring(0,
 55  
                                     index), queries[i].substring(index + 1));
 56  
                         }
 57  
                     }
 58  
                 }
 59  0
                 for (final Iterator it = getUrlParameters().entrySet()
 60  0
                         .iterator(); it.hasNext();) {
 61  0
                     final Map.Entry entry = (Map.Entry) it.next();
 62  0
                     final String key = (String) entry.getKey();
 63  0
                     final UrlParameter parameter = (UrlParameter) entry
 64  
                             .getValue();
 65  0
                     final String[] values = parameter.getValues();
 66  0
                     for (int i = 0; i < values.length; i++) {
 67  0
                         final String value = values[i];
 68  0
                         portletUrl.setParameter(key, value);
 69  
                     }
 70  
                 }
 71  0
                 return portletUrl.toString();
 72  
             }
 73  
         }
 74  0
         return super.build();
 75  
 
 76  
     }
 77  
 
 78  
     protected String calculatePath(String currentPath, String path) {
 79  0
         if (path.startsWith("/")) {
 80  0
             return path;
 81  
         }
 82  0
         String[] cPaths = currentPath.split("/");
 83  0
         String[] nPaths = path.split("/");
 84  0
         ArrayList list = new ArrayList();
 85  0
         for (int i = 0; i < cPaths.length - 1; i++) {
 86  0
             if (!cPaths[i].equals("")) {
 87  0
                 list.add(cPaths[i]);
 88  
             }
 89  
         }
 90  0
         for (int i = 0; i < nPaths.length; i++) {
 91  0
             if ("..".equals(nPaths[i])) {
 92  0
                 int size = list.size();
 93  0
                 if (size > 0) {
 94  0
                     list.remove(size - 1);
 95  
                 }
 96  0
             } else if (".".equals(nPaths[i])) {
 97  
                 // nothing
 98  
             } else {
 99  0
                 if (!nPaths[i].equals("")) {
 100  0
                     list.add(nPaths[i]);
 101  
                 }
 102  
             }
 103  
         }
 104  0
         StringBuffer p = new StringBuffer("");
 105  0
         for (int i = 0; i < list.size(); i++) {
 106  0
             p.append("/");
 107  0
             p.append(list.get(i));
 108  
         }
 109  0
         return p.toString();
 110  
     }
 111  
 }