Subversion Repositories XServices

Rev

Rev 147 | Go to most recent revision | Details | Compare with Previous | 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;
116 brianR 44
/**
45
 * @author Brian Rosenberger, bru(at)brutex.de
46
 *
47
 */
92 brianR 48
 
116 brianR 49
 
50
 
92 brianR 51
@XmlRootElement
147 brianR 52
@XmlType(propOrder={"name", "path", "filesize", "owner", "lastModifiedDate", "createdDate", "lastAccessDate", "mimeType",
53
					"readonly", "hidden", "directory", "symbolicLink", "archive", "system", "downloadUrl"})
54
@XmlAccessorType(XmlAccessType.PROPERTY)
116 brianR 55
public class FileInfoType
56
{
57
  private String name;
58
  private String path;
59
  private long filesize;
147 brianR 60
  private boolean isReadonly;
61
  private boolean isHidden;
116 brianR 62
  private boolean isDirectory;
147 brianR 63
  private boolean isSymbolicLink;
64
  private boolean isArchive;
65
  private boolean isSystem;
66
  private GregorianCalendar lastModifiedDate;
67
  private GregorianCalendar createdDate;
68
  private GregorianCalendar lastAccessDate;
69
  private String owner;
70
  private String mimeType;
71
  private URI downloadUrl;
72
 
92 brianR 73
 
116 brianR 74
  public FileInfoType()
75
  {
76
  }
147 brianR 77
 
78
  public FileInfoType(Path p) throws IOException {
79
	  	this.name = p.getFileName().toString();
80
	    this.path = p.toAbsolutePath().toString().replace('\\', '/');
92 brianR 81
 
147 brianR 82
 
83
		BasicFileAttributeView basicView = Files.getFileAttributeView(p, BasicFileAttributeView.class);
84
		BasicFileAttributes basic;
85
		basic = basicView.readAttributes();
86
 
87
		this.isDirectory = basic.isDirectory();
88
		this.isSymbolicLink = basic.isSymbolicLink();
89
		this.filesize = basic.size();
90
		this.lastModifiedDate = (GregorianCalendar) GregorianCalendar.getInstance();
91
		this.lastModifiedDate.setTimeInMillis(basic.lastModifiedTime().toMillis());
92
		this.createdDate = (GregorianCalendar) GregorianCalendar.getInstance();
93
		this.createdDate.setTimeInMillis(basic.creationTime().toMillis());
94
		this.lastAccessDate = (GregorianCalendar) GregorianCalendar.getInstance();
95
		this.lastAccessDate.setTimeInMillis(basic.lastAccessTime().toMillis());
96
 
97
		// Try to set the Mime Type for that file
98
		// or default to octet-stream
99
		if(!isDirectory) {
100
			this.mimeType = Files.probeContentType(p);
101
			if(this.mimeType==null) mimeType = "application/octet-stream";
102
		} else {
103
			this.mimeType = null;
104
		}
105
 
106
		// Set the file/ directory owner
107
		this.owner = Files.getOwner(p).getName();
108
 
109
		//Dos specific Attributes
110
		DosFileAttributeView dosView = Files.getFileAttributeView(p, DosFileAttributeView.class);
111
		if(dosView != null) {
112
			DosFileAttributes dos = dosView.readAttributes();
113
			this.isReadonly = dos.isReadOnly();
114
			this.isHidden = dos.isHidden();
115
			this.isArchive = dos.isArchive();
116
			this.isSystem = dos.isSystem();
117
		}
118
 
119
		//POSIX specific Attributes
120
		PosixFileAttributeView posixView = Files.getFileAttributeView(p, PosixFileAttributeView.class);
121
		if(posixView != null) {
122
			PosixFileAttributes posix = posixView.readAttributes();
123
			//TODO: Unix specific file attributes
124
		}
125
 
116 brianR 126
  }
92 brianR 127
 
147 brianR 128
 
129
  public FileInfoType(Path file, URI downloadURL) throws IOException {
130
	  this(file);
131
	  try {
132
		this.downloadUrl = URI.create(downloadURL+URLEncoder.encode(this.path, "UTF-8"));
133
	} catch (UnsupportedEncodingException e) {
134
		// TODO Auto-generated catch block
135
		e.printStackTrace();
136
	}
116 brianR 137
  }
92 brianR 138
 
147 brianR 139
/**
140
 * @return the name
141
 */
142
  @XmlElement(name="name")
143
public String getName() {
144
	return name;
145
}
92 brianR 146
 
147 brianR 147
/**
148
 * @return the path
149
 */
150
	@XmlElement(name = "path")
151
public String getPath() {
152
	return path;
153
}
92 brianR 154
 
147 brianR 155
/**
156
 * @return the filesize
157
 */
158
@XmlElement(name="filesize")
159
public long getFilesize() {
160
	return filesize;
161
}
92 brianR 162
 
147 brianR 163
/**
164
 * @return the isReadonly
165
 */
166
@XmlElement(name="isReadonly")
167
public boolean isReadonly() {
168
	return isReadonly;
169
}
92 brianR 170
 
147 brianR 171
/**
172
 * @return the isHidden
173
 */
174
@XmlElement(name="isHidden")
175
public boolean isHidden() {
176
	return isHidden;
177
}
92 brianR 178
 
147 brianR 179
/**
180
 * @return the isDirectory
181
 */
182
@XmlElement(name="isDirectory")
183
public boolean isDirectory() {
184
	return isDirectory;
185
}
116 brianR 186
 
147 brianR 187
/**
188
 * @return the isSymbolicLink
189
 */
190
@XmlElement(name="isSymbolicLink")
191
public boolean isSymbolicLink() {
192
	return isSymbolicLink;
193
}
194
 
195
/**
196
 * @return the isArchive
197
 */
198
@XmlElement(name="isArchive")
199
public boolean isArchive() {
200
	return isArchive;
201
}
202
 
203
/**
204
 * @return the isSystem
205
 */
206
@XmlElement(name="isSystem")
207
public boolean isSystem() {
208
	return isSystem;
209
}
210
 
211
/**
212
 * @return the lastModifiedDate
213
 */
214
@XmlElement(name="lastModifiedDate")
215
public GregorianCalendar getLastModifiedDate() {
216
	return lastModifiedDate;
217
}
218
 
219
/**
220
 * @return the createdDate
221
 */
222
@XmlElement(name="createdDate")
223
public GregorianCalendar getCreatedDate() {
224
	return createdDate;
225
}
226
 
227
/**
228
 * @return the lastAccessDate
229
 */
230
@XmlElement(name="lastAccessDate")
231
public GregorianCalendar getLastAccessDate() {
232
	return lastAccessDate;
233
}
234
 
235
/**
236
 * @return the owner
237
 */
238
@XmlElement(name="owner")
239
public String getOwner() {
240
	return owner;
241
}
242
 
243
/**
244
 * @return the mimeType
245
 */
246
@XmlElement(name="mimeType")
247
public String getMimeType() {
248
	return mimeType;
249
}
250
 
251
/**
252
 * @return the downloadUrl
253
 */
254
@XmlElement(name="downloadUrl")
255
public URI getDownloadUrl() {
256
	return downloadUrl;
257
}
258
 
259
 
116 brianR 260
}