Skip to content

Commit 026d702

Browse files
Redesign the classes for sdk and test cases
1 parent 89c788a commit 026d702

19 files changed

+1487
-1068
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
// This code is auto-generated, do not modify
17+
package com.spectralogic.ds3client.metadata;
18+
19+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataRestoreListener;
20+
import com.spectralogic.ds3client.networking.Metadata;
21+
22+
import java.text.SimpleDateFormat;
23+
import java.util.Calendar;
24+
25+
import static com.spectralogic.ds3client.utils.MetadataKeyConstants.*;
26+
27+
28+
public class MACMetaDataRestore extends PosixMetaDataRestore {
29+
public MACMetaDataRestore(final Metadata metadata, final String filePath, final String localOS, final MetaDataRestoreListener metaDataRestoreListener) {
30+
super(metadata, filePath, localOS,metaDataRestoreListener);
31+
}
32+
33+
/**
34+
* Return date in specified format.
35+
*
36+
* @param milliSeconds Date in milliseconds
37+
* @param dateFormat Date format
38+
* @return String representing date in specified format
39+
*/
40+
public static String getDate(final long milliSeconds, final String dateFormat) {
41+
// Create a DateFormatter object for displaying date in specified format.
42+
final SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
43+
// Create a calendar object that will convert the date and time value in milliseconds to date.
44+
final Calendar calendar = Calendar.getInstance();
45+
calendar.setTimeInMillis(milliSeconds);
46+
return formatter.format(calendar.getTime());
47+
}
48+
49+
@Override
50+
public void restoreFileTimes() {
51+
String creationTime = null;
52+
if (metadata.get(KEY_CREATION_TIME).size() > 0) {
53+
creationTime = metadata.get(KEY_CREATION_TIME).get(0);
54+
}
55+
String accessTime = null;
56+
if (metadata.get(KEY_ACCESS_TIME).size() > 0) {
57+
accessTime = metadata.get(KEY_ACCESS_TIME).get(0);
58+
}
59+
String modifiedTime = null;
60+
if (metadata.get(KEY_LAST_MODIFIED_TIME).size() > 0) {
61+
modifiedTime = metadata.get(KEY_LAST_MODIFIED_TIME).get(0);
62+
}
63+
if (modifiedTime != null && creationTime != null && accessTime != null) {
64+
restoreCreationTimeMAC(objectName, creationTime);
65+
restoreModifiedTimeMAC(objectName, modifiedTime);
66+
}
67+
68+
}
69+
70+
/**
71+
* Restore creation time in mac only if target creation is before current creation time
72+
*
73+
* @param objectName path of the object where we need to restore
74+
* @param creationTime creation time got from server
75+
*/
76+
private void restoreCreationTimeMAC(final String objectName, final String creationTime) {
77+
try {
78+
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-t", getDate(Long.parseLong(creationTime), "YYYYMMddHHmm"), objectName);
79+
processBuilder.start();
80+
} catch (final Exception e) {
81+
LOG.error("Unable to restore creation time", e);
82+
metaDataRestoreListener.metadataRestoreFailed("Unable to restore creation time ::"+e.getMessage());
83+
}
84+
85+
}
86+
87+
/**
88+
* Restore modified time in case of MAC using touch command
89+
*
90+
* @param objectName path of the object where we need to restore
91+
* @param modifiedTime modified time need to restore
92+
*/
93+
private void restoreModifiedTimeMAC(final String objectName, final String modifiedTime) {
94+
try {
95+
final ProcessBuilder processBuilder = new ProcessBuilder("touch", "-mt", getDate(Long.parseLong(modifiedTime), "YYYYMMddHHmm"), objectName);
96+
processBuilder.start();
97+
} catch (final Exception e) {
98+
LOG.error("Unable to restore modified time", e);
99+
metaDataRestoreListener.metadataRestoreFailed("Unable to restore modified time ::"+e.getMessage());
100+
}
101+
102+
}
103+
}
Lines changed: 52 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,37 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
// This code is auto-generated, do not modify
117
package com.spectralogic.ds3client.metadata;
218

319

420
import com.google.common.collect.ImmutableMap;
521
import com.spectralogic.ds3client.helpers.Ds3ClientHelpers;
22+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataStore;
23+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataStoreListner;
624
import com.spectralogic.ds3client.utils.MetaDataUtil;
725
import org.slf4j.Logger;
826
import org.slf4j.LoggerFactory;
927

1028
import java.io.IOException;
1129
import java.nio.file.Files;
1230
import java.nio.file.Path;
13-
import java.nio.file.attribute.*;
14-
import java.util.*;
31+
import java.nio.file.attribute.BasicFileAttributes;
32+
import java.nio.file.attribute.PosixFileAttributes;
33+
import java.util.Map;
34+
import java.util.Set;
1535

1636
/**
1737
* Implementation of MetaDataAcess Interface
@@ -20,9 +40,11 @@
2040
public class MetaDataAccessImpl implements Ds3ClientHelpers.MetadataAccess {
2141
static private final Logger LOG = LoggerFactory.getLogger(MetaDataAccessImpl.class);
2242
private final Map<String, Path> fileMapper;
43+
private final MetaDataStoreListner metaDataStoreListner;
2344

24-
public MetaDataAccessImpl(final Map<String, Path> fileMapper) {
45+
public MetaDataAccessImpl(final Map<String, Path> fileMapper ,final MetaDataStoreListner metaDataStoreListner) {
2546
this.fileMapper = fileMapper;
47+
this.metaDataStoreListner = metaDataStoreListner;
2648
}
2749

2850
@Override
@@ -32,134 +54,57 @@ public Map<String, String> getMetadataValue(final String filename) {
3254
return storeMetaData(file).build();
3355
} catch (final Exception e) {
3456
LOG.error("failed to store Metadata", e);
57+
metaDataStoreListner.onMetaDataFailed("Unable to get MetaData"+e.getMessage());
3558
return null;
3659
}
3760
}
3861

3962
/**
40-
* @param file
41-
* @return
63+
* @param file local path of file
64+
* @return map builder containing the data to be stored on server
4265
*/
4366
private ImmutableMap.Builder<String, String> storeMetaData(final Path file) {
4467
final ImmutableMap.Builder<String, String> metadata = new ImmutableMap.Builder<>();
4568
new ImmutableMap.Builder<String, String>();
46-
4769
try {
48-
final MetaDataUtil metadataUtil = new MetaDataUtil(metadata);
49-
final Set<String> sets = metadataUtil.getSupportedFileAttributes(file);
50-
final String os = metadataUtil.getOS();
51-
metadataUtil.saveOSMetaData();
52-
53-
for (final String set : sets) {
54-
switch (set) {
70+
//get local os name
71+
final String localOSName = MetaDataUtil.getOS();
72+
final Set<String> setFileAttributes = MetaDataUtil.getSupportedFileAttributes(file);
73+
//get metadata store based on os type
74+
final MetaDataStore metaDataStore = new MetaDataStoreFactory().getOsSpecificMetadataStore(localOSName, metadata);
75+
metaDataStore.saveOSMetaData(localOSName);
76+
for (final String fileAttributeType : setFileAttributes) {
77+
switch (fileAttributeType) {
5578
case "basic":
5679
final BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);
57-
metadataUtil.saveCreationTimeMetaData(attr);
58-
metadataUtil.saveAccessTimeMetaData(attr);
59-
metadataUtil.saveLastModifiedTime(attr);
60-
61-
if (os.contains("Windows")) {
62-
saveWindowsfilePermissions(file, metadata);
63-
}
64-
65-
break;
80+
metaDataStore.saveCreationTimeMetaData(attr);
81+
metaDataStore.saveAccessTimeMetaData(attr);
82+
metaDataStore.saveLastModifiedTime(attr);
83+
if (metaDataStore instanceof WindowsMetaDataStore) {
84+
((WindowsMetaDataStore) metaDataStore).saveWindowsfilePermissions(file);
85+
((WindowsMetaDataStore) metaDataStore).saveWindowsDescriptors(file);
86+
((WindowsMetaDataStore) metaDataStore).saveFlagMetaData(file);
6687

67-
case "owner":
68-
if (os.contains("Windows")) {
69-
metadataUtil.saveWindowsDescriptors(file);
7088
}
71-
72-
break;
73-
74-
case "user":
75-
break;
76-
77-
case "dos":
78-
if (os.contains("Windows")) {
79-
metadataUtil.saveFlagMetaData(file);
80-
}
81-
8289
break;
83-
8490
case "posix":
8591
final PosixFileAttributes attrPosix = Files.readAttributes(file, PosixFileAttributes.class);
86-
metadataUtil.saveUserId(file);
87-
metadataUtil.saveGroupId(file);
88-
metadataUtil.saveModeMetaData(file);
89-
metadataUtil.saveOwnerNameMetaData(attrPosix);
90-
metadataUtil.saveGroupNameMetaData(attrPosix);
91-
metadataUtil.savePosixPermssionsMeta(attrPosix);
92-
92+
if (metaDataStore instanceof PosixMetaDataStore) {
93+
((PosixMetaDataStore) metaDataStore).saveUserId(file);
94+
((PosixMetaDataStore) metaDataStore).saveGroupId(file);
95+
((PosixMetaDataStore) metaDataStore).saveModeMetaData(file);
96+
((PosixMetaDataStore) metaDataStore).saveOwnerNameMetaData(attrPosix);
97+
((PosixMetaDataStore) metaDataStore).saveGroupNameMetaData(attrPosix);
98+
((PosixMetaDataStore) metaDataStore).savePosixPermssionsMeta(attrPosix);
99+
}
93100
break;
94101
}
95102
}
96103
} catch (final IOException ioe) {
97104
LOG.error("unable to get metadata", ioe);
105+
metaDataStoreListner.onMetaDataFailed("Unable to get MetaData"+ioe.getMessage());
98106
}
99107
return metadata;
100108
}
101109

102-
103-
/**
104-
* if os is windows then posix will not be called and we need to find permission in different manner
105-
*
106-
* @param file
107-
* @param metadata
108-
*/
109-
private void saveWindowsfilePermissions(final Path file, final ImmutableMap.Builder<String, String> metadata) {
110-
try {
111-
final AclFileAttributeView view = Files.getFileAttributeView(file, AclFileAttributeView.class);
112-
final List<AclEntry> aclEntries = view.getAcl();
113-
Set<AclEntryPermission> aclEntryPermissions;
114-
String userType = "";
115-
String userDisplay = "";
116-
StringBuilder permission = null;
117-
final StringBuilder userList = new StringBuilder();
118-
final StringBuilder userDisplayList = new StringBuilder();
119-
final Map<String, Set<Integer>> stringSetMap = new HashMap<String, Set<Integer>>();
120-
for (final AclEntry aclEntry : aclEntries) {
121-
userDisplay = aclEntry.principal().getName().split("\\\\")[1];
122-
permission = new StringBuilder();
123-
Set<Integer> newSet = stringSetMap.get(userDisplay);
124-
aclEntryPermissions = aclEntry.permissions();
125-
if (newSet == null) {
126-
newSet = new HashSet<Integer>();
127-
}
128-
for (final AclEntryPermission aclEntryPermission : aclEntryPermissions) {
129-
newSet.add(aclEntryPermission.ordinal());
130-
}
131-
stringSetMap.put(userDisplay, newSet);
132-
}
133-
final Set<String> keys = stringSetMap.keySet();
134-
Set<Integer> ordinals;
135-
int userCount = 1;
136-
for (final String key : keys) {
137-
int index = 1;
138-
ordinals = stringSetMap.get(key);
139-
userType = key.replaceAll(" ", "").toLowerCase();
140-
permission = new StringBuilder();
141-
for (final int ord : ordinals) {
142-
if (ordinals.size() == index) {
143-
permission.append(ord);
144-
} else {
145-
permission.append(ord + "-");
146-
}
147-
index++;
148-
}
149-
if (keys.size() == userCount) {
150-
userDisplayList.append(key);
151-
userList.append(userType);
152-
} else {
153-
userDisplayList.append(key + "-");
154-
userList.append(userType + "-");
155-
}
156-
metadata.put("x-amz-meta-ds3-" + userType, permission.toString());
157-
userCount++;
158-
}
159-
metadata.put("x-amz-meta-ds3-userList", userList.toString());
160-
metadata.put("x-amz-meta-ds3-userListDisplay", userDisplayList.toString());
161-
} catch (final Exception e) {
162-
LOG.error("Unable to get list of users or their permissions", e);
163-
}
164-
}
165110
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
// This code is auto-generated, do not modify
17+
package com.spectralogic.ds3client.metadata;
18+
19+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataRestore;
20+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataRestoreListener;
21+
import com.spectralogic.ds3client.networking.Metadata;
22+
23+
24+
public class MetaDataRestoreFactory {
25+
public MetaDataRestore getOSSpecificMetadataRestore(final Metadata metadata, final String filePath, final String localOS , final MetaDataRestoreListener metaDataRestoreListener) {
26+
if (localOS.contains("Windows")) {
27+
return new WindowsMetaDataRestore(metadata, filePath, localOS,metaDataRestoreListener);
28+
} else if (localOS.contains("Mac")) {
29+
return new MACMetaDataRestore(metadata, filePath, localOS,metaDataRestoreListener);
30+
} else {
31+
return new PosixMetaDataRestore(metadata, filePath, localOS,metaDataRestoreListener);
32+
}
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
// This code is auto-generated, do not modify
17+
package com.spectralogic.ds3client.metadata;
18+
19+
import com.google.common.collect.ImmutableMap;
20+
import com.spectralogic.ds3client.metadata.interfaces.MetaDataStore;
21+
22+
23+
public class MetaDataStoreFactory
24+
{
25+
public MetaDataStore getOsSpecificMetadataStore(final String osName, final ImmutableMap.Builder<String, String> metadataMap)
26+
{
27+
if(osName.contains("Windows"))
28+
return new WindowsMetaDataStore(metadataMap);
29+
else
30+
return new PosixMetaDataStore(metadataMap);
31+
}
32+
}

0 commit comments

Comments
 (0)