Subversion Repositories XServices

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
116 brianR 1
/*
2
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
3
 *
4
 *   Licensed under the Apache License, Version 2.0 (the "License");
5
 *   you may not use this file except in compliance with the License.
6
 *   You may obtain a copy of the License at
7
 *
8
 *       http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *   Unless required by applicable law or agreed to in writing, software
11
 *   distributed under the License is distributed on an "AS IS" BASIS,
12
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *   See the License for the specific language governing permissions and
14
 *   limitations under the License.
15
 */
16
 
92 brianR 17
package net.brutex.xservices.types;
18
 
19
import java.io.File;
147 brianR 20
import java.io.IOException;
21
import java.io.UnsupportedEncodingException;
22
import java.net.URI;
23
import java.net.URLEncoder;
24
import java.nio.file.FileSystems;
25
import java.nio.file.Files;
26
import java.nio.file.LinkOption;
27
import java.nio.file.Path;
28
import java.nio.file.attribute.BasicFileAttributeView;
29
import java.nio.file.attribute.BasicFileAttributes;
30
import java.nio.file.attribute.DosFileAttributeView;
31
import java.nio.file.attribute.DosFileAttributes;
32
import java.nio.file.attribute.FileTime;
33
import java.nio.file.attribute.PosixFileAttributeView;
34
import java.nio.file.attribute.PosixFileAttributes;
35
import java.nio.file.attribute.UserPrincipal;
36
import java.util.GregorianCalendar;
37
 
38
import javax.xml.bind.annotation.XmlAccessType;
39
import javax.xml.bind.annotation.XmlAccessorOrder;
40
import javax.xml.bind.annotation.XmlAccessorType;
92 brianR 41
import javax.xml.bind.annotation.XmlElement;
42
import javax.xml.bind.annotation.XmlRootElement;
147 brianR 43
import javax.xml.bind.annotation.XmlType;
44
 
45
import com.sun.xml.bind.XmlAccessorFactory;
116 brianR 46
/**
47
 * @author Brian Rosenberger, bru(at)brutex.de
48
 *
49
 */
92 brianR 50
 
116 brianR 51
 
52
 
92 brianR 53
@XmlRootElement
147 brianR 54
@XmlType(propOrder={"name", "path", "filesize", "owner", "lastModifiedDate", "createdDate", "lastAccessDate", "mimeType",
55
					"readonly", "hidden", "directory", "symbolicLink", "archive", "system", "downloadUrl"})
56
@XmlAccessorType(XmlAccessType.PROPERTY)
116 brianR 57
public class FileInfoType
58
{
59
  private String name;
60
  private String path;
61
  private long filesize;
147 brianR 62
  private boolean isReadonly;
63
  private boolean isHidden;
116 brianR 64
  private boolean isDirectory;
147 brianR 65
  private boolean isSymbolicLink;
66
  private boolean isArchive;
67
  private boolean isSystem;
68
  private GregorianCalendar lastModifiedDate;
69
  private GregorianCalendar createdDate;
70
  private GregorianCalendar lastAccessDate;
71
  private String owner;
72
  private String mimeType;
73
  private URI downloadUrl;
74
 
92 brianR 75
 
116 brianR 76
  public FileInfoType()
77
  {
78
  }
147 brianR 79
 
80
  public FileInfoType(Path p) throws IOException {
81
	  	this.name = p.getFileName().toString();
82
	    this.path = p.toAbsolutePath().toString().replace('\\', '/');
92 brianR 83
 
147 brianR 84
 
85
		BasicFileAttributeView basicView = Files.getFileAttributeView(p, BasicFileAttributeView.class);
86
		BasicFileAttributes basic;
87
		basic = basicView.readAttributes();
88
 
89
		this.isDirectory = basic.isDirectory();
90
		this.isSymbolicLink = basic.isSymbolicLink();
91
		this.filesize = basic.size();
92
		this.lastModifiedDate = (GregorianCalendar) GregorianCalendar.getInstance();
93
		this.lastModifiedDate.setTimeInMillis(basic.lastModifiedTime().toMillis());
94
		this.createdDate = (GregorianCalendar) GregorianCalendar.getInstance();
95
		this.createdDate.setTimeInMillis(basic.creationTime().toMillis());
96
		this.lastAccessDate = (GregorianCalendar) GregorianCalendar.getInstance();
97
		this.lastAccessDate.setTimeInMillis(basic.lastAccessTime().toMillis());
98
 
99
		// Try to set the Mime Type for that file
100
		// or default to octet-stream
101
		if(!isDirectory) {
102
			this.mimeType = Files.probeContentType(p);
103
			if(this.mimeType==null) mimeType = "application/octet-stream";
104
		} else {
105
			this.mimeType = null;
106
		}
107
 
108
		// Set the file/ directory owner
109
		this.owner = Files.getOwner(p).getName();
110
 
111
		//Dos specific Attributes
112
		DosFileAttributeView dosView = Files.getFileAttributeView(p, DosFileAttributeView.class);
113
		if(dosView != null) {
114
			DosFileAttributes dos = dosView.readAttributes();
115
			this.isReadonly = dos.isReadOnly();
116
			this.isHidden = dos.isHidden();
117
			this.isArchive = dos.isArchive();
118
			this.isSystem = dos.isSystem();
119
		}
120
 
121
		//POSIX specific Attributes
122
		PosixFileAttributeView posixView = Files.getFileAttributeView(p, PosixFileAttributeView.class);
123
		if(posixView != null) {
124
			PosixFileAttributes posix = posixView.readAttributes();
125
			//TODO: Unix specific file attributes
126
		}
127
 
116 brianR 128
  }
92 brianR 129
 
147 brianR 130
 
131
  public FileInfoType(Path file, URI downloadURL) throws IOException {
132
	  this(file);
133
	  try {
134
		this.downloadUrl = URI.create(downloadURL+URLEncoder.encode(this.path, "UTF-8"));
135
	} catch (UnsupportedEncodingException e) {
136
		// TODO Auto-generated catch block
137
		e.printStackTrace();
138
	}
116 brianR 139
  }
92 brianR 140
 
147 brianR 141
/**
142
 * @return the name
143
 */
144
  @XmlElement(name="name")
145
public String getName() {
146
	return name;
147
}
92 brianR 148
 
147 brianR 149
/**
150
 * @return the path
151
 */
152
	@XmlElement(name = "path")
153
public String getPath() {
154
	return path;
155
}
92 brianR 156
 
147 brianR 157
/**
158
 * @return the filesize
159
 */
160
@XmlElement(name="filesize")
161
public long getFilesize() {
162
	return filesize;
163
}
92 brianR 164
 
147 brianR 165
/**
166
 * @return the isReadonly
167
 */
168
@XmlElement(name="isReadonly")
169
public boolean isReadonly() {
170
	return isReadonly;
171
}
92 brianR 172
 
147 brianR 173
/**
174
 * @return the isHidden
175
 */
176
@XmlElement(name="isHidden")
177
public boolean isHidden() {
178
	return isHidden;
179
}
92 brianR 180
 
147 brianR 181
/**
182
 * @return the isDirectory
183
 */
184
@XmlElement(name="isDirectory")
185
public boolean isDirectory() {
186
	return isDirectory;
187
}
116 brianR 188
 
147 brianR 189
/**
190
 * @return the isSymbolicLink
191
 */
192
@XmlElement(name="isSymbolicLink")
193
public boolean isSymbolicLink() {
194
	return isSymbolicLink;
195
}
196
 
197
/**
198
 * @return the isArchive
199
 */
200
@XmlElement(name="isArchive")
201
public boolean isArchive() {
202
	return isArchive;
203
}
204
 
205
/**
206
 * @return the isSystem
207
 */
208
@XmlElement(name="isSystem")
209
public boolean isSystem() {
210
	return isSystem;
211
}
212
 
213
/**
214
 * @return the lastModifiedDate
215
 */
216
@XmlElement(name="lastModifiedDate")
217
public GregorianCalendar getLastModifiedDate() {
218
	return lastModifiedDate;
219
}
220
 
221
/**
222
 * @return the createdDate
223
 */
224
@XmlElement(name="createdDate")
225
public GregorianCalendar getCreatedDate() {
226
	return createdDate;
227
}
228
 
229
/**
230
 * @return the lastAccessDate
231
 */
232
@XmlElement(name="lastAccessDate")
233
public GregorianCalendar getLastAccessDate() {
234
	return lastAccessDate;
235
}
236
 
237
/**
238
 * @return the owner
239
 */
240
@XmlElement(name="owner")
241
public String getOwner() {
242
	return owner;
243
}
244
 
245
/**
246
 * @return the mimeType
247
 */
248
@XmlElement(name="mimeType")
249
public String getMimeType() {
250
	return mimeType;
251
}
252
 
253
/**
254
 * @return the downloadUrl
255
 */
256
@XmlElement(name="downloadUrl")
257
public URI getDownloadUrl() {
258
	return downloadUrl;
259
}
260
 
261
 
116 brianR 262
}