View Javadoc
1   /**
2    * This file Copyright (c) 2014-2015 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.dam.external.app.contentconnector;
35  
36  import java.util.regex.Pattern;
37  
38  import info.magnolia.dam.api.AssetProvider;
39  import info.magnolia.dam.api.AssetProviderRegistry;
40  import info.magnolia.dam.api.ItemKey;
41  import info.magnolia.dam.api.PathAwareAssetProvider;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.ui.vaadin.integration.contentconnector.AbstractContentConnector;
44  
45  import javax.inject.Inject;
46  
47  import org.apache.commons.lang3.StringUtils;
48  import org.slf4j.Logger;
49  import org.slf4j.LoggerFactory;
50  
51  import com.vaadin.data.Item;
52  import com.vaadin.data.util.BeanItem;
53  
54  /**
55   * {@link info.magnolia.ui.vaadin.integration.contentconnector.ContentConnector} implementation
56   * which works over Assets .<br>
57   * Object itemId are {@link info.magnolia.dam.api.Item#getItemKey()}.
58   */
59  public class AssetContentConnector extends AbstractContentConnector {
60  
61      private static final Pattern FRAGMENT_PATTERN = Pattern.compile("[a-zA-Z0-9]+\\..+");
62  
63  
64      private static final Logger log = LoggerFactory.getLogger(AssetContentConnector.class);
65  
66      private final AssetProviderRegistry assetProviderRegistry;
67      private AssetProvider assetProvider;
68  
69      @Inject
70      public AssetContentConnector(ConfiguredAssetContentConnectorDefinition contentConnectorDefinition, ComponentProvider componentProvider, AssetProviderRegistry assetProviderRegistry) {
71          super(contentConnectorDefinition, componentProvider);
72          this.assetProviderRegistry = assetProviderRegistry;
73          this.assetProvider = this.assetProviderRegistry.getProviderById(contentConnectorDefinition.getAssetProviderId());
74      }
75  
76      @Override
77      public ConfiguredAssetContentConnectorDefinition getContentConnectorDefinition() {
78          return (ConfiguredAssetContentConnectorDefinition) super.getContentConnectorDefinition();
79      }
80  
81      public AssetProvider getAssetProvider() {
82          return this.assetProvider;
83      }
84  
85      public void setAssetProvider(String providerId) {
86          this.assetProvider = assetProviderRegistry.getProviderById(providerId);
87      }
88  
89      @Override
90      public Object getDefaultItemId() {
91          return assetProvider.getRootFolder().getItemKey();
92      }
93  
94      @Override
95      public String getItemUrlFragment(Object itemId) {
96          if (itemId instanceof ItemKey) {
97              ItemKey key = (ItemKey) itemId;
98              if (!assetProvider.getIdentifier().equals(key.getProviderId())) {
99                  assetProvider = assetProviderRegistry.getProviderById(key.getProviderId());
100             }
101             try {
102                 String identifier = assetProvider.getIdentifier();
103                 info.magnolia.dam.api.Item item = assetProvider.getItem((ItemKey) itemId);
104                 return identifier + "." + item.getPath();
105             } catch (AssetProvider.AssetNotFoundException e) {
106                 log.debug("Failed to retrieve item with given id: " + e.getMessage(), e);
107             } catch (AssetProvider.IllegalItemKeyException e) {
108                 log.debug("Failed to convert item id to URL fragment: " + e.getMessage(), e);
109             }
110         }
111         return null;
112     }
113 
114     @Override
115     public Object getItemIdByUrlFragment(String urlFragment) {
116         String assetProviderId = StringUtils.substringBefore(urlFragment, ".");
117         if (FRAGMENT_PATTERN.matcher(urlFragment).matches()) {
118             String path = StringUtils.substringAfter(urlFragment, ".");
119 
120             setAssetProvider(assetProviderId);
121             if (assetProvider instanceof PathAwareAssetProvider) {
122                 try {
123                     Object item = ((PathAwareAssetProvider) assetProvider).getItem(path);
124                     return ((info.magnolia.dam.api.Item) item).getItemKey();
125                 } catch (AssetProvider.AssetNotFoundException e) {
126                     log.debug("Failed to obtain asset id for fragment: " + e.getMessage(), e);
127                 } catch (PathAwareAssetProvider.PathNotFoundException e) {
128                     log.debug("Failed to obtain asset id for fragment: " + e.getMessage(), e);
129                 }
130             }
131         }
132         return null;
133     }
134 
135     @Override
136     public Item getItem(Object itemId) {
137         if (itemId == null || !(itemId instanceof ItemKey)) {
138             return null;
139         }
140         ItemKey key = (ItemKey) itemId;
141         if (assetProvider == null || !assetProvider.getIdentifier().equals(key.getProviderId())) {
142             assetProvider = assetProviderRegistry.getProviderById(key.getProviderId());
143         }
144         try {
145             info.magnolia.dam.api.Item item = assetProvider.getItem(key);
146             if (item != null) {
147                 return new BeanItem<info.magnolia.dam.api.Item>(item);
148             }
149         } catch (AssetProvider.AssetNotFoundException e) {
150             log.debug("Failed to find item for id: " + itemId, e.getMessage());
151         } catch (AssetProvider.IllegalItemKeyException e) {
152             log.debug("Failed to find item for id: " + itemId, e.getMessage());
153         }
154         return null;
155     }
156 
157     @Override
158     public Object getItemId(Item item) {
159         if (item instanceof BeanItem) {
160             return ((BeanItem<info.magnolia.dam.api.Item>) item).getBean().getItemKey();
161         }
162         return null;
163     }
164 
165     @Override
166     public boolean canHandleItem(Object itemId) {
167         return itemId instanceof ItemKey;
168     }
169 
170 }