001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.atom;
016    
017    import java.io.InputStream;
018    
019    import java.util.Date;
020    
021    /**
022     * @author Igor Spasic
023     */
024    public abstract class BaseAtomCollectionAdapter<E>
025            implements AtomCollectionAdapter<E> {
026    
027            public void deleteEntry(
028                            String resourceName, AtomRequestContext atomRequestContext)
029                    throws AtomException {
030    
031                    try {
032                            doDeleteEntry(resourceName, atomRequestContext);
033                    }
034                    catch (Exception e) {
035                            Class<?> clazz = e.getClass();
036    
037                            String className = clazz.getSimpleName();
038    
039                            if (className.startsWith("NoSuch")) {
040                                    throw new AtomException(SC_NOT_FOUND);
041                            }
042    
043                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
044                    }
045            }
046    
047            public E getEntry(
048                            String resourceName, AtomRequestContext atomRequestContext)
049                    throws AtomException {
050    
051                    try {
052                            return doGetEntry(resourceName, atomRequestContext);
053                    }
054                    catch (Exception e) {
055                            Class<?> clazz = e.getClass();
056    
057                            String className = clazz.getSimpleName();
058    
059                            if (className.startsWith("NoSuch")) {
060                                    throw new AtomException(SC_NOT_FOUND);
061                            }
062    
063                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
064                    }
065            }
066    
067            public Iterable<E> getFeedEntries(AtomRequestContext atomRequestContext)
068                    throws AtomException {
069    
070                    try {
071                            return doGetFeedEntries(atomRequestContext);
072                    }
073                    catch (Exception e) {
074                            Class<?> clazz = e.getClass();
075    
076                            String className = clazz.getSimpleName();
077    
078                            if (className.startsWith("NoSuch")) {
079                                    throw new AtomException(SC_NOT_FOUND);
080                            }
081    
082                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
083                    }
084            }
085    
086            public String getMediaContentType(E entry) {
087                    throw new UnsupportedOperationException();
088            }
089    
090            @SuppressWarnings("unused")
091            public String getMediaName(E entry) throws AtomException {
092                    throw new UnsupportedOperationException();
093            }
094    
095            @SuppressWarnings("unused")
096            public InputStream getMediaStream(E entry) throws AtomException {
097                    throw new UnsupportedOperationException();
098            }
099    
100            public E postEntry(
101                            String title, String summary, String content, Date date,
102                            AtomRequestContext atomRequestContext)
103                    throws AtomException {
104    
105                    try {
106                            return doPostEntry(
107                                    title, summary, content, date, atomRequestContext);
108                    }
109                    catch (Exception e) {
110                            Class<?> clazz = e.getClass();
111    
112                            String className = clazz.getSimpleName();
113    
114                            if (className.startsWith("NoSuch")) {
115                                    throw new AtomException(SC_NOT_FOUND);
116                            }
117    
118                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
119                    }
120            }
121    
122            public E postMedia(
123                            String mimeType, String slug, InputStream inputStream,
124                            AtomRequestContext atomRequestContext)
125                    throws AtomException {
126    
127                    try {
128                            return doPostMedia(mimeType, slug, inputStream, atomRequestContext);
129                    }
130                    catch (Exception e) {
131                            Class<?> clazz = e.getClass();
132    
133                            String className = clazz.getSimpleName();
134    
135                            if (className.startsWith("NoSuch")) {
136                                    throw new AtomException(SC_NOT_FOUND);
137                            }
138    
139                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
140                    }
141            }
142    
143            public void putEntry(
144                            E entry, String title, String summary, String content, Date date,
145                            AtomRequestContext atomRequestContext)
146                    throws AtomException {
147    
148                    try {
149                            doPutEntry(
150                                    entry, title, summary, content, date, atomRequestContext);
151                    }
152                    catch (Exception e) {
153                            Class<?> clazz = e.getClass();
154    
155                            String className = clazz.getSimpleName();
156    
157                            if (className.startsWith("NoSuch")) {
158                                    throw new AtomException(SC_NOT_FOUND);
159                            }
160    
161                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
162                    }
163            }
164    
165            public void putMedia(
166                            E entry, String mimeType, String slug, InputStream inputStream,
167                            AtomRequestContext atomRequestContext)
168                    throws AtomException {
169    
170                    try {
171                            doPutMedia(entry, mimeType, slug, inputStream, atomRequestContext);
172                    }
173                    catch (Exception e) {
174                            Class<?> clazz = e.getClass();
175    
176                            String className = clazz.getSimpleName();
177    
178                            if (className.startsWith("NoSuch")) {
179                                    throw new AtomException(SC_NOT_FOUND);
180                            }
181    
182                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
183                    }
184            }
185    
186            protected void doDeleteEntry(
187                            String resourceName, AtomRequestContext atomRequestContext)
188                    throws Exception {
189    
190                    throw new UnsupportedOperationException();
191            }
192    
193            protected abstract E doGetEntry(
194                            String resourceName, AtomRequestContext atomRequestContext)
195                    throws Exception;
196    
197            protected abstract Iterable<E> doGetFeedEntries(
198                            AtomRequestContext atomRequestContext)
199                    throws Exception;
200    
201            protected E doPostEntry(
202                            String title, String summary, String content, Date date,
203                            AtomRequestContext atomRequestContext)
204                    throws Exception {
205    
206                    throw new UnsupportedOperationException();
207            }
208    
209            protected E doPostMedia(
210                            String mimeType, String slug, InputStream inputStream,
211                            AtomRequestContext atomRequestContext)
212                    throws Exception {
213    
214                    throw new UnsupportedOperationException();
215            }
216    
217            protected void doPutEntry(
218                            E entry, String title, String summary, String content, Date date,
219                            AtomRequestContext atomRequestContext)
220                    throws Exception {
221    
222                    throw new UnsupportedOperationException();
223            }
224    
225            protected void doPutMedia(
226                            E entry, String mimeType, String slug, InputStream inputStream,
227                            AtomRequestContext atomRequestContext)
228                    throws Exception {
229    
230                    throw new UnsupportedOperationException();
231            }
232    
233    }