Skip to content

Commit d50d7ce

Browse files
authored
Merge pull request #109 from masterdany88/release_1.6.0
FileUploader, support for unauthorized response and custom message body
2 parents d10bd0e + 3ec77ef commit d50d7ce

File tree

4 files changed

+139
-11
lines changed

4 files changed

+139
-11
lines changed

src/main/java/gwt/material/design/addins/client/fileuploader/MaterialFileUploader.java

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ protected native void initDropzone(Element e, Element template, String previews,
140140
var previewContainer = $wnd.jQuery("#" + previews).html();
141141
previewNode.id = "";
142142
var previewTemplate = previewNode.parent().html();
143+
var globalResponse = "";
143144
144145
var totalFiles = 0;
145146
var zdrop = new $wnd.Dropzone(e, {
@@ -206,8 +207,18 @@ protected native void initDropzone(Element e, Element template, String previews,
206207
if(file.xhr !== undefined) {
207208
code = file.xhr.status;
208209
}
210+
if(response.indexOf("401") >= 0) {
211+
response = "Unautharized. Probably Your's session expired. Log in and try again.";
212+
globalResponse = response;
213+
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireUnauthorizedEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, file.xhr.status, file.xhr.statusText, response);
214+
}
209215
if(response.indexOf("404") >= 0) {
210216
response = "There's a problem uploading your file.";
217+
globalResponse = response;
218+
}
219+
if(response.indexOf("500") >= 0) {
220+
response = "There's a problem uploading your file.";
221+
globalResponse = response;
211222
}
212223
file.previewElement.querySelector("#error-message").innerHTML = response;
213224
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireErrorEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, code, response);
@@ -226,11 +237,12 @@ protected native void initDropzone(Element e, Element template, String previews,
226237
});
227238
228239
zdrop.on('success', function (file, response) {
229-
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireSuccessEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, file.xhr.status, response);
240+
globalResponse = response;
241+
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireSuccessEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, file.xhr.status, file.xhr.statusText, response);
230242
});
231243
232244
zdrop.on('complete', function (file) {
233-
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireCompleteEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, file.xhr.status, file.xhr.statusText);
245+
that.@gwt.material.design.addins.client.fileuploader.MaterialFileUploader::fireCompleteEvent(*)(file.name , file.lastModifiedDate , file.size , file.type, file.xhr.status, file.xhr.statusText, globalResponse);
234246
});
235247
236248
zdrop.on('canceled', function (file) {
@@ -511,8 +523,25 @@ public void onError(ErrorEvent<UploadFile> event) {
511523
}
512524

513525
@Override
514-
public void fireErrorEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage) {
515-
ErrorEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage));
526+
public void fireErrorEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody) {
527+
ErrorEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage, responseBody));
528+
}
529+
530+
@Override
531+
public HandlerRegistration addUnauthorizedHandler(final UnauthorizedEvent.UnauthorizedHandler<UploadFile> handler) {
532+
return addHandler(new UnauthorizedEvent.UnauthorizedHandler<UploadFile>() {
533+
@Override
534+
public void onUnauthorized(UnauthorizedEvent<UploadFile> event) {
535+
if (isEnabled()) {
536+
handler.onUnauthorized(event);
537+
}
538+
}
539+
}, UnauthorizedEvent.getType());
540+
}
541+
542+
@Override
543+
public void fireUnauthorizedEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody) {
544+
UnauthorizedEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage, responseBody));
516545
}
517546

518547
@Override
@@ -562,8 +591,8 @@ public void onSuccess(SuccessEvent<UploadFile> event) {
562591
}
563592

564593
@Override
565-
public void fireSuccessEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage) {
566-
SuccessEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage));
594+
public void fireSuccessEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody) {
595+
SuccessEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage, responseBody));
567596
}
568597

569598
@Override
@@ -579,8 +608,8 @@ public void onComplete(CompleteEvent<UploadFile> event) {
579608
}
580609

581610
@Override
582-
public void fireCompleteEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage) {
583-
CompleteEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage));
611+
public void fireCompleteEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody) {
612+
CompleteEvent.fire(this, new UploadFile(fileName, new Date(lastModified), Double.parseDouble(size), type), new UploadResponse(responseCode, responseMessage, responseBody));
584613
}
585614

586615
@Override

src/main/java/gwt/material/design/addins/client/fileuploader/base/HasFileUpload.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ public interface HasFileUpload<T> extends HasHandlers {
9898
*/
9999
HandlerRegistration addErrorHandler(ErrorEvent.ErrorHandler<T> handler);
100100

101-
void fireErrorEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage);
101+
void fireErrorEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody);
102+
/**
103+
* An unauthorized error occured. Probably because of session expiration.
104+
* Receives the errorMessage as second parameter and if the error was due to
105+
* the XMLHttpRequest the xhr object as third.
106+
*
107+
* @param handler
108+
*/
109+
HandlerRegistration addUnauthorizedHandler(UnauthorizedEvent.UnauthorizedHandler<T> handler);
110+
111+
void fireUnauthorizedEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody);
102112

103113
/**
104114
* Called with the total uploadProgress (0-100). This event can be used to show the overall upload progress of all files.
@@ -123,15 +133,15 @@ public interface HasFileUpload<T> extends HasHandlers {
123133
*/
124134
HandlerRegistration addSuccessHandler(SuccessEvent.SuccessHandler<T> handler);
125135

126-
void fireSuccessEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage);
136+
void fireSuccessEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody);
127137

128138
/**
129139
* Called when the upload was either successful or erroneous.
130140
* @param handler
131141
*/
132142
HandlerRegistration addCompleteHandler(CompleteEvent.CompleteHandler<T> handler);
133143

134-
void fireCompleteEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage);
144+
void fireCompleteEvent(String fileName, String lastModified, String size, String type, String responseCode, String responseMessage, String responseBody);
135145

136146
/**
137147
* Called when a file upload gets canceled.

src/main/java/gwt/material/design/addins/client/fileuploader/base/UploadResponse.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ public class UploadResponse {
2929

3030
private String code;
3131
private String message;
32+
private String body;
3233

3334
public UploadResponse(String code, String message) {
3435
this.code = code;
3536
this.message = message;
3637
}
3738

39+
public UploadResponse(String code, String message, String body) {
40+
this.code = code;
41+
this.message = message;
42+
this.body = body;
43+
}
44+
3845
public String getCode() {
3946
return code;
4047
}
@@ -50,4 +57,12 @@ public String getMessage() {
5057
public void setMessage(String message) {
5158
this.message = message;
5259
}
60+
61+
public String getBody() {
62+
return body;
63+
}
64+
65+
public void setBody(String body) {
66+
this.body = body;
67+
}
5368
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package gwt.material.design.addins.client.fileuploader.events;
2+
3+
/*
4+
* #%L
5+
* GwtMaterial
6+
* %%
7+
* Copyright (C) 2015 - 2016 GwtMaterialDesign
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import gwt.material.design.addins.client.fileuploader.base.HasFileUpload;
24+
import gwt.material.design.addins.client.fileuploader.base.UploadResponse;
25+
26+
import com.google.gwt.event.shared.EventHandler;
27+
import com.google.gwt.event.shared.GwtEvent;
28+
29+
public class UnauthorizedEvent<T> extends GwtEvent<UnauthorizedEvent.UnauthorizedHandler<T>> {
30+
31+
private static Type<UnauthorizedHandler<?>> TYPE;
32+
33+
public interface UnauthorizedHandler<T> extends EventHandler {
34+
void onUnauthorized(UnauthorizedEvent<T> event);
35+
}
36+
37+
public static <T> void fire(HasFileUpload<T> source, T target, UploadResponse response) {
38+
if (TYPE != null) {
39+
UnauthorizedEvent<T> event = new UnauthorizedEvent<T>(target, response);
40+
source.fireEvent(event);
41+
}
42+
}
43+
44+
public static Type<UnauthorizedHandler<?>> getType() {
45+
return TYPE != null ? TYPE : (TYPE = new Type<UnauthorizedHandler<?>>());
46+
}
47+
48+
private final T target;
49+
private final UploadResponse response;
50+
51+
protected UnauthorizedEvent(T target, UploadResponse response) {
52+
this.target = target;
53+
this.response = response;
54+
}
55+
56+
@Override
57+
public final Type<UnauthorizedHandler<T>> getAssociatedType() {
58+
return (Type) TYPE;
59+
}
60+
61+
public T getTarget() {
62+
return target;
63+
}
64+
65+
public UploadResponse getResponse() {
66+
return response;
67+
}
68+
69+
@Override
70+
protected void dispatch(UnauthorizedHandler<T> handler) {
71+
handler.onUnauthorized(this);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)