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                    String href = HttpUtil.encodePath(parentPath);
067    
068                    if (Validator.isNotNull(name)) {
069                            href += StringPool.SLASH + HttpUtil.encodeURL(name, true);
070                    }
071    
072                    _href = href;
073    
074                    if (createDate == null) {
075                            _createDate = new Date();
076                    }
077                    else {
078                            _createDate = createDate;
079                    }
080    
081                    if (modifiedDate == null) {
082                            _modifiedDate = new Date();
083                    }
084                    else {
085                            _modifiedDate = modifiedDate;
086                    }
087            }
088    
089            @Override
090            public String getClassName() {
091                    return _className;
092            }
093    
094            @Override
095            @SuppressWarnings("unused")
096            public InputStream getContentAsStream() throws WebDAVException {
097                    return null;
098            }
099    
100            @Override
101            public String getContentType() {
102                    return ContentTypes.HTTPD_UNIX_DIRECTORY;
103            }
104    
105            @Override
106            public String getCreateDate() {
107                    return _createDateFormatter.format(_createDate);
108            }
109    
110            @Override
111            public String getDisplayName() {
112                    return _displayName;
113            }
114    
115            @Override
116            public String getHREF() {
117                    return _href;
118            }
119    
120            @Override
121            public Lock getLock() {
122                    return null;
123            }
124    
125            @Override
126            public Object getModel() {
127                    return _model;
128            }
129    
130            @Override
131            public String getModifiedDate() {
132                    return _modifiedDateFormatter.format(_modifiedDate);
133            }
134    
135            @Override
136            public long getPrimaryKey() {
137                    return _primaryKey;
138            }
139    
140            @Override
141            public long getSize() {
142                    return _size;
143            }
144    
145            @Override
146            public boolean isCollection() {
147                    return true;
148            }
149    
150            @Override
151            public boolean isLocked() {
152                    return false;
153            }
154    
155            @Override
156            public void setClassName(String className) {
157                    _className = className;
158            }
159    
160            @Override
161            public void setModel(Object model) {
162                    _model = model;
163            }
164    
165            @Override
166            public void setPrimaryKey(long primaryKey) {
167                    _primaryKey = primaryKey;
168            }
169    
170            private static final Format _createDateFormatter =
171                    FastDateFormatFactoryUtil.getSimpleDateFormat(
172                            "yyyy-MM-dd'T'HH:mm:ss'Z'", LocaleUtil.US, TimeZoneUtil.GMT);
173            private static final Format _modifiedDateFormatter =
174                    FastDateFormatFactoryUtil.getSimpleDateFormat(
175                            "EEE, dd MMM yyyy HH:mm:ss zzz", LocaleUtil.US, TimeZoneUtil.GMT);
176    
177            private String _className;
178            private final Date _createDate;
179            private final String _displayName;
180            private final String _href;
181            private Object _model;
182            private final Date _modifiedDate;
183            private long _primaryKey = -1;
184            private final long _size;
185    
186    }