View Javadoc
1   package org.exoplatform.services.wcm.friendly.impl;
2   /*
3    * Copyright (C) 2003-2009 eXo Platform SAS.
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Affero General Public License
7    * as published by the Free Software Foundation; either version 3
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, see<http://www.gnu.org/licenses/>.
17   */
18  
19  import java.util.LinkedHashMap;
20  import java.util.Map;
21  
22  import org.exoplatform.container.xml.InitParams;
23  import org.exoplatform.container.xml.ObjectParameter;
24  import org.exoplatform.container.xml.ValueParam;
25  import org.exoplatform.management.annotations.Managed;
26  import org.exoplatform.management.annotations.ManagedDescription;
27  import org.exoplatform.management.annotations.ManagedName;
28  import org.exoplatform.management.jmx.annotations.NameTemplate;
29  import org.exoplatform.management.jmx.annotations.Property;
30  import org.exoplatform.management.rest.annotations.RESTEndpoint;
31  import org.exoplatform.services.log.ExoLogger;
32  import org.exoplatform.services.log.Log;
33  import org.exoplatform.services.wcm.friendly.FriendlyService;
34  import org.exoplatform.services.wcm.friendly.impl.FriendlyConfig.Friendly;
35  
36  @Managed
37  @NameTemplate({@Property(key = "view", value = "portal"), @Property(key = "service", value = "friendly"),
38     @Property(key = "type", value = "content")})
39  @ManagedDescription("Friendly service")
40  @RESTEndpoint(path = "friendlyservice")
41  public class FriendlyServiceImpl implements FriendlyService {
42  
43    private String servletName = "content";
44  
45    private boolean isEnabled = false;
46  
47    private Map<String, String> friendlies;
48    private Map<String, String> unfriendlies;
49  
50    private static final Log LOG  = ExoLogger.getLogger(FriendlyServiceImpl.class.getName());
51  
52    public FriendlyServiceImpl(InitParams initParams) {
53      friendlies = new LinkedHashMap<String, String>(5);
54      unfriendlies = new LinkedHashMap<String, String>(5);
55      if (initParams!=null) init(initParams);
56    }
57  
58    private void init(InitParams initParams) {
59  
60        ValueParam enabled = initParams.getValueParam("enabled");
61        ValueParam servletName = initParams.getValueParam("servletName");
62  
63        if (enabled!=null) {
64          if ("true".equals(enabled.getValue())) {
65            isEnabled = true;
66          }
67        }
68        if (LOG.isInfoEnabled()) LOG.info("isEnabled:"+isEnabled);
69        if (servletName!=null) {
70          this.servletName = servletName.getValue();
71        }
72        if (LOG.isInfoEnabled()) LOG.info("servletName:"+this.servletName);
73  
74        ObjectParameter objectParam = initParams.getObjectParam("friendlies.configuration");
75        if (objectParam != null) {
76          FriendlyConfig config = (FriendlyConfig)objectParam.getObject();
77          for (Friendly friendly:config.getFriendlies()) {
78            this.addFriendly(friendly.getFriendlyUri(), friendly.getUnfriendlyUri());
79          }
80        }
81    }
82  
83    public void addConfiguration(FriendlyPlugin plugin) {
84      this.init(plugin.getInitParams());
85    }
86  
87  
88      @Managed
89      @ManagedDescription("Is the service enabled ?")
90    public boolean isEnabled() {
91      return isEnabled;
92    }
93  
94      @Managed
95      @ManagedDescription("Is the service enabled ?")
96    public void setEnabled(@ManagedDescription("Enable/Disable this service ?") @ManagedName("isEnabled") boolean isEnabled) {
97      this.isEnabled = isEnabled;
98    }
99  
100     @Managed
101     @ManagedDescription("The servlet name referenced in this service")
102   public String getServletName() {
103     return servletName;
104   }
105 
106   public void setServletName(String servletName) {
107     this.servletName = servletName;
108   }
109 
110     @Managed
111     @ManagedDescription("Add a new friendly in the list")
112   public void addFriendly(@ManagedDescription("The friendly Uri") @ManagedName("friendlyUri") String friendlyUri,
113       @ManagedDescription("The unfriendly Uri") @ManagedName("unfriendlyUri") String unfriendlyUri) {
114     if (!friendlies.containsKey(friendlyUri)) {
115       if (LOG.isInfoEnabled()) LOG.info("addFriendly::"+friendlyUri+"::"+unfriendlyUri+ "::");
116       this.friendlies.put(friendlyUri, unfriendlyUri);
117       this.unfriendlies.put(unfriendlyUri, friendlyUri);
118     }
119   }
120 
121   public String getFriendlyUri(String unfriendlyUri) {
122 
123     if (!isEnabled) return unfriendlyUri;
124 
125     for (String unf : unfriendlies.keySet()) {
126       if (unfriendlyUri.contains(unf)) {
127         String fr = unfriendlies.get(unf);
128         return unfriendlyUri.replace(unf, "/"+getServletName()+"/"+fr);
129       }
130     }
131 
132     return unfriendlyUri;
133   }
134 
135   public String getUnfriendlyUri(String friendlyUri) {
136     if (!isEnabled) return friendlyUri;
137 
138     String friendly = "/"+getServletName()+"/";
139     if (friendlyUri.contains(friendly)) {
140       int start = friendlyUri.indexOf(friendly) + friendly.length();
141       int end = friendlyUri.substring(start).indexOf("/");
142       if (end > -1) {
143       String furi = friendlyUri.substring(start, start+end);
144         if (friendlies.containsKey(furi)) {
145           String unf = friendlies.get(furi);
146           return friendlyUri.replace(friendly + furi, unf);
147         }
148       }
149     }
150 
151     return friendlyUri;
152   }
153 
154     @Managed
155     @ManagedDescription("Remove a friendly in the list")
156   public void removeFriendly(@ManagedDescription("The friendly Uri") @ManagedName("friendlyUri") String friendlyUri) {
157     if (friendlies.containsKey(friendlyUri)) {
158       String unf = this.friendlies.get(friendlyUri);
159       friendlies.remove(friendlyUri);
160       unfriendlies.remove(unf);
161     }
162   }
163 
164     @Managed
165     @ManagedDescription("The list of registered friendlies")
166   public Map<String, String> getFriendlies() {
167     return friendlies;
168   }
169 
170 }