-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from takipi/feature/search-file-access
Feature/search file access
- Loading branch information
Showing
15 changed files
with
258 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/takipi/oss/storage/data/simple/SimpleSearchRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.takipi.oss.storage.data.simple; | ||
|
||
import com.takipi.oss.storage.data.EncodingType; | ||
|
||
public class SimpleSearchRequest { | ||
public EncodingType encodingType; | ||
public String name; | ||
public String baseSearchPath; | ||
public boolean preventDuplicates; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/takipi/oss/storage/data/simple/SimpleSearchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.takipi.oss.storage.data.simple; | ||
|
||
public class SimpleSearchResponse { | ||
String data; | ||
String path; | ||
|
||
public SimpleSearchResponse(String data, String path) { | ||
this.data = data; | ||
this.path = path; | ||
} | ||
|
||
public String getData() { | ||
return data; | ||
} | ||
|
||
public String getPath() { | ||
return path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/main/java/com/takipi/oss/storage/resources/fs/JsonSimpleSearchStorageResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.takipi.oss.storage.resources.fs; | ||
|
||
import java.io.File; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.codahale.metrics.annotation.Timed; | ||
import com.google.common.base.Predicate; | ||
import com.takipi.oss.storage.TakipiStorageConfiguration; | ||
import com.takipi.oss.storage.data.simple.SimpleSearchRequest; | ||
import com.takipi.oss.storage.data.simple.SimpleSearchResponse; | ||
import com.takipi.oss.storage.helper.FilesystemUtil; | ||
import com.takipi.oss.storage.resources.fs.base.SimpleFileSystemStorageResource; | ||
|
||
@Path("/storage/v1/json/simplesearch") | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON) | ||
public class JsonSimpleSearchStorageResource extends SimpleFileSystemStorageResource { | ||
private static final Logger logger = LoggerFactory.getLogger(JsonSimpleSearchStorageResource.class); | ||
|
||
public JsonSimpleSearchStorageResource(TakipiStorageConfiguration configuration) { | ||
super(configuration); | ||
} | ||
|
||
@POST | ||
@Timed | ||
public Response post(SimpleSearchRequest request) { | ||
try { | ||
return handleResponse(request); | ||
} catch (Exception e) { | ||
return Response.serverError().entity("Problem simple searching").build(); | ||
} | ||
} | ||
|
||
private Response handleResponse(SimpleSearchRequest request) { | ||
try { | ||
File searchRoot = new File(fs.getRoot(), FilesystemUtil.fixPath(request.baseSearchPath)); | ||
|
||
ResourceFileCallback fileCallback = new ResourceFileCallback(request.name, request.preventDuplicates); | ||
FilesystemUtil.listFilesRecursively(searchRoot, fileCallback); | ||
File result = fileCallback.getFoundFile(); | ||
|
||
if (result == null) { | ||
return searchFailed(request.name); | ||
} | ||
|
||
String relFSPath = result.getAbsolutePath().replace(fs.getRoot().getAbsolutePath(), ""); | ||
String data = FilesystemUtil.read(fs, relFSPath, request.encodingType); | ||
|
||
if (data == null) { | ||
return searchFailed(request.name); | ||
} | ||
|
||
return Response.ok(new SimpleSearchResponse(data, relFSPath.replace(request.name, ""))).build(); | ||
|
||
} catch (Exception e) { | ||
logger.error("Problem getting: " + request.name, e); | ||
return Response.serverError().entity("Problem getting " + request.name).build(); | ||
} | ||
} | ||
|
||
private Response searchFailed(String name) { | ||
logger.warn("File not found: {}", name); | ||
return Response.status(404).entity("File not found" + name).build(); | ||
} | ||
|
||
private static class ResourceFileCallback implements Predicate<File> | ||
{ | ||
private final String resourceName; | ||
private final boolean preventDuplicates; | ||
|
||
private File foundFile; | ||
|
||
protected ResourceFileCallback(String resourceName, boolean preventDuplicates) | ||
{ | ||
this.resourceName = resourceName; | ||
this.preventDuplicates = preventDuplicates; | ||
|
||
this.foundFile = null; | ||
} | ||
|
||
@Override | ||
public boolean apply(File file) | ||
{ | ||
if (!resourceName.equals(file.getName())) | ||
{ | ||
return false; | ||
} | ||
|
||
if ((preventDuplicates) && | ||
(foundFile != null)) | ||
{ | ||
foundFile = null; // never find more than one result if preventing duplicates | ||
return true; | ||
} | ||
|
||
foundFile = file; | ||
|
||
return !preventDuplicates; // if we don't prevent duplicates, we stop right now | ||
} | ||
|
||
public File getFoundFile() | ||
{ | ||
return foundFile; | ||
} | ||
} | ||
} |
14 changes: 0 additions & 14 deletions
14
src/main/java/com/takipi/oss/storage/resources/fs/base/FileSystemStorageResource.java
This file was deleted.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
src/main/java/com/takipi/oss/storage/resources/fs/base/FolderFileSystemStorageResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.takipi.oss.storage.resources.fs.base; | ||
|
||
import com.takipi.oss.storage.TakipiStorageConfiguration; | ||
import com.takipi.oss.storage.fs.folder.FolderFilesystem; | ||
|
||
public abstract class FolderFileSystemStorageResource<T> { | ||
protected final FolderFilesystem<T> fs; | ||
|
||
public FolderFileSystemStorageResource(TakipiStorageConfiguration configuration) { | ||
this.fs = getNewFileSystem(configuration); | ||
} | ||
|
||
protected abstract FolderFilesystem<T> getNewFileSystem(TakipiStorageConfiguration configuration); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.