-
Notifications
You must be signed in to change notification settings - Fork 1
CELDEV-1006 - refactor AttachmentURLCommand to ResourceUrlService #297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
80c1f47
1280d39
80b0d1a
47750ab
588f034
150a7fc
3b0ec74
9e3352c
b1c77ba
37b6138
ab8fd7c
504b79c
b9feaa7
8933716
69aed61
0f4570c
ee3a2bd
a74d3f9
c62b5f6
09a4c02
c507cc8
6f4b916
229455d
f2fa74c
334d07e
eb05687
094d2c0
38fdb38
4c02d4f
4c2a217
2ccb773
d05cae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| package com.celements.filebase.references; | ||
|
|
||
| import static com.google.common.base.Preconditions.*; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.annotation.concurrent.NotThreadSafe; | ||
| import javax.validation.constraints.NotEmpty; | ||
| import javax.validation.constraints.NotNull; | ||
| import javax.ws.rs.core.UriBuilder; | ||
|
|
||
| import org.xwiki.model.reference.DocumentReference; | ||
|
|
||
| import com.celements.model.util.ModelUtils; | ||
| import com.google.common.base.Strings; | ||
| import com.google.errorprone.annotations.Immutable; | ||
| import com.xpn.xwiki.web.Utils; | ||
|
|
||
| @Immutable | ||
| public final class FileReference implements Serializable { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| public enum FileReferenceType { | ||
| ON_DISK, ATTACHMENT, EXTERNAL; | ||
| } | ||
|
|
||
| @NotThreadSafe | ||
| public static final class Builder { | ||
|
|
||
| private static final Pattern ATTACHMENT_LINK_PATTERN = Pattern.compile( | ||
| "([\\w\\-]*:)?([\\w\\-]*\\.[\\w\\-]*){1};.*"); | ||
| private static final Pattern ON_DISK_LINK_PATTERN = Pattern.compile("^:[/\\w\\-\\.]*"); | ||
|
|
||
| private String name; | ||
| private FileReferenceType type; | ||
| private DocumentReference docRef; | ||
| private String fullPath; | ||
| private String queryString; | ||
|
|
||
| @NotNull | ||
| private static String getAttachmentName(@NotEmpty String link) { | ||
| return link.split(";")[1]; | ||
| } | ||
|
|
||
| @NotNull | ||
| private static String getPathFileName(@NotEmpty String link) { | ||
| String[] linkParts = link.split("/"); | ||
| return linkParts[linkParts.length - 1]; | ||
| } | ||
|
|
||
| private static boolean isAttachmentLink(@Nullable String link) { | ||
| if (link != null) { | ||
| return ATTACHMENT_LINK_PATTERN.matcher(link.trim()).matches(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private static boolean isOnDiskLink(@Nullable String link) { | ||
| if (link != null) { | ||
| return ON_DISK_LINK_PATTERN.matcher(link.trim()).matches(); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| @NotNull | ||
| private static DocumentReference getPageDocRef(@NotNull String link) { | ||
| return Utils.getComponent(ModelUtils.class).resolveRef(link.split(";")[0], | ||
| DocumentReference.class); | ||
| } | ||
|
|
||
| @NotNull | ||
| private static FileReferenceType getTypeOfLink(@NotEmpty String link) { | ||
| if (isOnDiskLink(link)) { | ||
| return FileReferenceType.ON_DISK; | ||
| } else if (isAttachmentLink(link)) { | ||
| return FileReferenceType.ATTACHMENT; | ||
| } | ||
| return FileReferenceType.EXTERNAL; | ||
| } | ||
|
|
||
| @NotNull | ||
| public Builder setFileName(@NotNull String fileName) { | ||
| checkNotNull(fileName); | ||
| this.name = fileName; | ||
| return this; | ||
| } | ||
|
|
||
| @NotNull | ||
| public Builder setType(@NotNull FileReferenceType type) { | ||
| checkNotNull(type); | ||
| this.type = type; | ||
| return this; | ||
| } | ||
|
|
||
| public void setDocRef(@NotNull DocumentReference docRef) { | ||
| checkNotNull(docRef); | ||
| this.docRef = docRef; | ||
| } | ||
|
|
||
| public void setFullPath(@NotEmpty String fullPath) { | ||
| checkArgument(!Strings.isNullOrEmpty(fullPath), "path may not be null or empty"); | ||
| this.fullPath = fullPath; | ||
| } | ||
|
|
||
| public void setQueryString(@Nullable String queryString) { | ||
| this.queryString = Strings.emptyToNull(queryString); | ||
| } | ||
|
|
||
| @NotNull | ||
| public FileReference build() { | ||
| return new FileReference(this); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private final String name; | ||
| private final FileReferenceType type; | ||
| private final DocumentReference docRef; | ||
| private final String fullPath; | ||
| private final String queryString; | ||
|
|
||
| private FileReference(Builder builder) { | ||
| this.name = builder.name; | ||
| this.type = builder.type; | ||
| this.fullPath = builder.fullPath; | ||
| this.docRef = builder.docRef; | ||
| this.queryString = builder.queryString; | ||
| } | ||
|
|
||
| @NotNull | ||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| @NotNull | ||
| public FileReferenceType getType() { | ||
| return type; | ||
| } | ||
|
|
||
| @Nullable | ||
| public DocumentReference getDocRef() { | ||
| return docRef; | ||
| } | ||
|
|
||
| @NotEmpty | ||
| public String getFullPath() { | ||
| return fullPath; | ||
| } | ||
|
|
||
| @NotNull | ||
| public Optional<String> getQueryString() { | ||
| return Optional.ofNullable(queryString); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder getUri() { | ||
| return UriBuilder.fromPath(fullPath).replaceQuery(queryString); | ||
| } | ||
|
|
||
| public boolean isAttachmentReference() { | ||
| return type == FileReferenceType.ATTACHMENT; | ||
| } | ||
|
|
||
| public boolean isOnDiskReference() { | ||
| return type == FileReferenceType.ON_DISK; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(name, type, docRef, fullPath); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object obj) { | ||
| return (obj instanceof FileReference) | ||
| && Objects.equals(((FileReference) obj).name, this.name) | ||
| && Objects.equals(((FileReference) obj).type, this.type) | ||
| && Objects.equals(((FileReference) obj).docRef, this.docRef) | ||
| && Objects.equals(((FileReference) obj).fullPath, this.fullPath); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "FileReference [name=" + name + ", type=" + type + ", docRef=" + docRef + ", fullPath=" | ||
| + fullPath + "]"; | ||
| } | ||
|
|
||
| @NotNull | ||
| public static FileReference of(@NotEmpty String link) { | ||
| checkArgument(!Strings.isNullOrEmpty(link), "link may not be empty"); | ||
| final String[] linkParts = link.split("\\?"); | ||
| Builder builder = new Builder(); | ||
| builder.setType(Builder.getTypeOfLink(linkParts[0])); | ||
| if (builder.type == FileReferenceType.ATTACHMENT) { | ||
| builder.setFileName(Builder.getAttachmentName(linkParts[0])); | ||
| builder.setDocRef(Builder.getPageDocRef(linkParts[0])); | ||
| } else { | ||
| builder.setFileName(Builder.getPathFileName(linkParts[0])); | ||
| builder.setFullPath(linkParts[0]); | ||
| } | ||
| if (linkParts.length > 1) { | ||
| builder.setQueryString(linkParts[1]); | ||
| } | ||
| return builder.build(); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.celements.filebase.uri; | ||
|
|
||
| import javax.validation.constraints.NotNull; | ||
|
|
||
| import com.celements.filebase.references.FileReference; | ||
|
|
||
| public class FileNotExistException extends Exception { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| private final FileReference fileReference; | ||
|
|
||
| public FileNotExistException(@NotNull FileReference fileRef) { | ||
| super("Url fileRef [" + fileRef + "] does not exist."); | ||
| this.fileReference = fileRef; | ||
| } | ||
|
|
||
| public FileNotExistException(@NotNull String message, @NotNull FileReference fileRef) { | ||
| super(message); | ||
| this.fileReference = fileRef; | ||
| } | ||
|
|
||
| public FileNotExistException(@NotNull String message, @NotNull FileReference fileRef, | ||
| Exception wrapExp) { | ||
| super(message, wrapExp); | ||
| this.fileReference = fileRef; | ||
| } | ||
|
|
||
| public FileReference getFileReference() { | ||
| return fileReference; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package com.celements.filebase.uri; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import javax.inject.Singleton; | ||
| import javax.validation.constraints.NotEmpty; | ||
| import javax.validation.constraints.NotNull; | ||
| import javax.ws.rs.core.UriBuilder; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.xwiki.component.annotation.Component; | ||
| import org.xwiki.component.annotation.Requirement; | ||
| import org.xwiki.script.service.ScriptService; | ||
|
|
||
| import com.celements.filebase.references.FileReference; | ||
| import com.google.common.base.Strings; | ||
|
|
||
| @Component(FileUriScriptService.NAME) | ||
| @Singleton | ||
| public class FileUriScriptService implements ScriptService { | ||
|
|
||
| public static final String NAME = "fileUri"; | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(FileUriScriptService.class); | ||
|
|
||
| @Requirement | ||
| private FileUrlServiceRole fileUriService; | ||
|
|
||
| @NotNull | ||
| public FileReference createFileReference(@NotEmpty String link) { | ||
| return FileReference.of(link); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createFileUrl(@Nullable String fileRef) { | ||
| return createFileUrl(fileRef, null); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createFileUrl(@Nullable String fileRef, @Nullable String action) { | ||
| return createFileUrl(fileRef, action, null); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createFileUrl(@Nullable String fileRef, @Nullable String action, | ||
| @Nullable String queryString) { | ||
| if (!Strings.isNullOrEmpty(fileRef)) { | ||
| return createFileUrl(createFileReference(fileRef), action, queryString); | ||
| } | ||
| return UriBuilder.fromPath(""); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createFileUrl(@Nullable FileReference fileRef, @Nullable String action, | ||
| @Nullable String queryString) { | ||
| if (fileRef != null) { | ||
| try { | ||
| return fileUriService.createFileUri(fileRef, Optional.ofNullable(action), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is the service method called
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to 1. There is a #try statement for velocity
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Optional.ofNullable(queryString)); | ||
| } catch (FileNotExistException exp) { | ||
| LOGGER.info("createFileUrl for [{}] with action [{}] an queryString [{}] failed.", fileRef, | ||
| action, queryString, exp); | ||
| } | ||
| } | ||
| return UriBuilder.fromPath(""); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder getFileURLPrefix() { | ||
| return fileUriService.getFileUrlPrefix(Optional.empty()); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder getFileURLPrefix(@Nullable String action) { | ||
| return fileUriService.getFileUrlPrefix(Optional.ofNullable(action)); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createAbsoluteFileUri(@Nullable String fileRef, @Nullable String action) { | ||
| return createAbsoluteFileUri(fileRef, action, null); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createAbsoluteFileUri(@Nullable String fileRef, String action, | ||
| @Nullable String queryString) { | ||
| if (!Strings.isNullOrEmpty(fileRef)) { | ||
| return createAbsoluteFileUri(createFileReference(fileRef), action, queryString); | ||
| } | ||
| return UriBuilder.fromPath(""); | ||
| } | ||
|
|
||
| @NotNull | ||
| public UriBuilder createAbsoluteFileUri(@Nullable FileReference fileRef, String action, | ||
| @Nullable String queryString) { | ||
| if (fileRef != null) { | ||
| return fileUriService.createAbsoluteFileUri(fileRef, Optional.ofNullable(action), | ||
| Optional.ofNullable(queryString)); | ||
| } | ||
| return UriBuilder.fromPath(""); | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.