001    /**
002     * Copyright (c) 2000-2012 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.getName();
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.getName();
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                            String className = e.getClass().getName();
075    
076                            if (className.startsWith("NoSuch")) {
077                                    throw new AtomException(SC_NOT_FOUND);
078                            }
079    
080                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
081                    }
082            }
083    
084            public String getMediaContentType(E entry) {
085                    throw new UnsupportedOperationException();
086            }
087    
088            @SuppressWarnings("unused")
089            public String getMediaName(E entry) throws AtomException {
090                    throw new UnsupportedOperationException();
091            }
092    
093            @SuppressWarnings("unused")
094            public InputStream getMediaStream(E entry) throws AtomException {
095                    throw new UnsupportedOperationException();
096            }
097    
098            public E postEntry(
099                            String title, String summary, String content, Date date,
100                            AtomRequestContext atomRequestContext)
101                    throws AtomException {
102    
103                    try {
104                            return doPostEntry(
105                                    title, summary, content, date, atomRequestContext);
106                    }
107                    catch (Exception e) {
108                            Class<?> clazz = e.getClass();
109    
110                            String className = clazz.getName();
111    
112                            if (className.startsWith("NoSuch")) {
113                                    throw new AtomException(SC_NOT_FOUND);
114                            }
115    
116                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
117                    }
118            }
119    
120            public E postMedia(
121                            String mimeType, String slug, InputStream inputStream,
122                            AtomRequestContext atomRequestContext)
123                    throws AtomException {
124    
125                    try {
126                            return doPostMedia(mimeType, slug, inputStream, atomRequestContext);
127                    }
128                    catch (Exception e) {
129                            Class<?> clazz = e.getClass();
130    
131                            String className = clazz.getName();
132    
133                            if (className.startsWith("NoSuch")) {
134                                    throw new AtomException(SC_NOT_FOUND);
135                            }
136    
137                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
138                    }
139            }
140    
141            public void putEntry(
142                            E entry, String title, String summary, String content, Date date,
143                            AtomRequestContext atomRequestContext)
144                    throws AtomException {
145    
146                    try {
147                            doPutEntry(
148                                    entry, title, summary, content, date, atomRequestContext);
149                    }
150                    catch (Exception e) {
151                            Class<?> clazz = e.getClass();
152    
153                            String className = clazz.getName();
154    
155                            if (className.startsWith("NoSuch")) {
156                                    throw new AtomException(SC_NOT_FOUND);
157                            }
158    
159                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
160                    }
161            }
162    
163            public void putMedia(
164                            E entry, String mimeType, String slug, InputStream inputStream,
165                            AtomRequestContext atomRequestContext)
166                    throws AtomException {
167    
168                    try {
169                            doPutMedia(entry, mimeType, slug, inputStream, atomRequestContext);
170                    }
171                    catch (Exception e) {
172                            Class<?> clazz = e.getClass();
173    
174                            String className = clazz.getName();
175    
176                            if (className.startsWith("NoSuch")) {
177                                    throw new AtomException(SC_NOT_FOUND);
178                            }
179    
180                            throw new AtomException(SC_INTERNAL_SERVER_ERROR, e);
181                    }
182            }
183    
184            protected void doDeleteEntry(
185                            String resourceName, AtomRequestContext atomRequestContext)
186                    throws Exception {
187    
188                    throw new UnsupportedOperationException();
189            }
190    
191            protected abstract E doGetEntry(
192                            String resourceName, AtomRequestContext atomRequestContext)
193                    throws Exception;
194    
195            protected abstract Iterable<E> doGetFeedEntries(
196                            AtomRequestContext atomRequestContext)
197                    throws Exception;
198    
199            protected E doPostEntry(
200                            String title, String summary, String content, Date date,
201                            AtomRequestContext atomRequestContext)
202                    throws Exception {
203    
204                    throw new UnsupportedOperationException();
205            }
206    
207            protected E doPostMedia(
208                            String mimeType, String slug, InputStream inputStream,
209                            AtomRequestContext atomRequestContext)
210                    throws Exception {
211    
212                    throw new UnsupportedOperationException();
213            }
214    
215            protected void doPutEntry(
216                            E entry, String title, String summary, String content, Date date,
217                            AtomRequestContext atomRequestContext)
218                    throws Exception {
219    
220                    throw new UnsupportedOperationException();
221            }
222    
223            protected void doPutMedia(
224                            E entry, String mimeType, String slug, InputStream inputStream,
225                            AtomRequestContext atomRequestContext)
226                    throws Exception {
227    
228                    throw new UnsupportedOperationException();
229            }
230    
231    }