001    /**
002     * Copyright (c) 2000-present 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.webdav;
016    
017    import com.liferay.portal.kernel.lock.Lock;
018    import com.liferay.portal.kernel.util.ContentTypes;
019    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.TimeZoneUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.io.InputStream;
027    
028    import java.text.Format;
029    
030    import java.util.Date;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Alexander Chow
035     */
036    public class BaseResourceImpl implements Resource {
037    
038            public BaseResourceImpl(String parentPath, long name, long displayName) {
039                    this(parentPath, String.valueOf(name), String.valueOf(displayName));
040            }
041    
042            public BaseResourceImpl(String parentPath, long name, String displayName) {
043                    this(parentPath, String.valueOf(name), displayName);
044            }
045    
046            public BaseResourceImpl(
047                    String parentPath, String name, String displayName) {
048    
049                    this(parentPath, name, displayName, null, null);
050            }
051    
052            public BaseResourceImpl(
053                    String parentPath, String name, String displayName, Date createDate,
054                    Date modifiedDate) {
055    
056                    this(parentPath, name, displayName, createDate, modifiedDate, 0);
057            }
058    
059            public BaseResourceImpl(
060                    String parentPath, String name, String displayName, Date createDate,
061                    Date modifiedDate, long size) {
062    
063                    _displayName = displayName;
064                    _size = size;
065    
066                    if (Validator.isNotNull(name)) {
067                            parentPath += StringPool.SLASH + HttpUtil.encodeURL(name);
068                    }
069    
070                    _href = HttpUtil.encodePath(parentPath);
071    
072                    if (createDate == null) {
073                            _createDate = new Date();
074                    }
075                    else {
076                            _createDate = createDate;
077                    }
078    
079                    if (modifiedDate == null) {
080                            _modifiedDate = new Date();
081                    }
082                    else {
083                            _modifiedDate = modifiedDate;
084                    }
085            }
086    
087            @Override
088            public String getClassName() {
089                    return _className;
090            }
091    
092            @Override
093            @SuppressWarnings("unused")
094            public InputStream getContentAsStream() throws WebDAVException {
095                    return null;
096            }
097    
098            @Override
099            public String getContentType() {
100                    return ContentTypes.HTTPD_UNIX_DIRECTORY;
101            }
102    
103            @Override
104            public String getCreateDate() {
105                    return _createDateFormatter.format(_createDate);
106            }
107    
108            @Override
109            public String getDisplayName() {
110                    return _displayName;
111            }
112    
113            @Override
114            public String getHREF() {
115                    return _href;
116            }
117    
118            @Override
119            public Lock getLock() {
120                    return null;
121            }
122    
123            @Override
124            public Object getModel() {
125                    return _model;
126            }
127    
128            @Override
129            public String getModifiedDate() {
130                    return _modifiedDateFormatter.format(_modifiedDate);
131            }
132    
133            @Override
134            public long getPrimaryKey() {
135                    return _primaryKey;
136            }
137    
138            @Override
139            public long getSize() {
140                    return _size;
141            }
142    
143            @Override
144            public boolean isCollection() {
145                    return true;
146            }
147    
148            @Override
149            public boolean isLocked() {
150                    return false;
151            }
152    
153            @Override
154            public void setClassName(String className) {
155                    _className = className;
156            }
157    
158            @Override
159            public void setModel(Object model) {
160                    _model = model;
161            }
162    
163            @Override
164            public void setPrimaryKey(long primaryKey) {
165                    _primaryKey = primaryKey;
166            }
167    
168            private static final Format _createDateFormatter =
169                    FastDateFormatFactoryUtil.getSimpleDateFormat(
170                            "yyyy-MM-dd'T'HH:mm:ss'Z'", LocaleUtil.US, TimeZoneUtil.GMT);
171            private static final Format _modifiedDateFormatter =
172                    FastDateFormatFactoryUtil.getSimpleDateFormat(
173                            "EEE, dd MMM yyyy HH:mm:ss zzz", LocaleUtil.US, TimeZoneUtil.GMT);
174    
175            private String _className;
176            private final Date _createDate;
177            private final String _displayName;
178            private final String _href;
179            private Object _model;
180            private final Date _modifiedDate;
181            private long _primaryKey = -1;
182            private final long _size;
183    
184    }