-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileUploadProxyUtils.java
More file actions
149 lines (137 loc) · 5.04 KB
/
FileUploadProxyUtils.java
File metadata and controls
149 lines (137 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package com.smartling.api.jobbatches.util;
import com.smartling.api.jobbatches.exceptions.JobBatchesApiExceptionMapper;
import com.smartling.api.v2.client.exception.RestApiExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.jboss.resteasy.annotations.providers.multipart.PartFilename;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.client.jaxrs.internal.ClientResponse;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput;
import javax.ws.rs.FormParam;
import javax.ws.rs.Path;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;
@Slf4j
public class FileUploadProxyUtils
{
private static final String CLIENT_LIB_ID = "smartling.client_lib_id";
public static void addClientLibIdIfNeeded(MultipartFormDataOutput requestData)
{
if (!requestData.getFormData().containsKey(CLIENT_LIB_ID))
{
String clientLibId = LibNameVersionHolder.getClientLibName() + "/" + LibNameVersionHolder.getClientLibVersion();
requestData.addFormData(CLIENT_LIB_ID, clientLibId, MediaType.TEXT_PLAIN_TYPE);
}
}
public static Response sendRequest(ResteasyWebTarget client, String path, String projectId, String batchUid, MultipartFormDataOutput output)
{
RestApiExceptionHandler exceptionHandler = new RestApiExceptionHandler(new JobBatchesApiExceptionMapper());
try
{
Response response = client.path(path)
.resolveTemplate("projectId", projectId)
.resolveTemplate("batchUid", batchUid)
.request()
.post(Entity.entity(output, MediaType.MULTIPART_FORM_DATA));
if (response.getStatus() != HttpStatus.SC_ACCEPTED)
{
throw new WebApplicationException(response);
}
return response;
}
catch (WebApplicationException e)
{
throw exceptionHandler.createRestApiException(e);
}
}
public static void getFields(Class<?> type, MultipartFormDataOutput output, Object obj)
{
for (Field field : type.getDeclaredFields())
{
field.setAccessible(true);
Object value = getFieldValue(field, obj);
if (value == null)
{
continue;
}
if (field.isAnnotationPresent(FormParam.class) && field.isAnnotationPresent(PartType.class))
{
FormParam param = field.getAnnotation(FormParam.class);
PartType partType = field.getAnnotation(PartType.class);
String filename = getFilename(field);
output.addFormData(param.value(), value, field.getType(), field.getGenericType(), MediaType.valueOf(partType.value()), filename);
}
if (field.getType().isAssignableFrom(Map.class))
{
Map<String, String> directives = (Map<String, String>) getFieldValue(field, obj);
if (directives == null)
{
continue;
}
for (Map.Entry<String, String> entry : directives.entrySet())
{
if (entry.getValue() == null)
{
continue;
}
output.addFormData(entry.getKey(), entry.getValue(), MediaType.TEXT_PLAIN_TYPE);
}
}
}
}
public static void releaseConnection(Response response)
{
if (response instanceof ClientResponse)
{
try
{
((ClientResponse) response).releaseConnection();
}
catch (IOException e)
{
log.warn("Failed to release connection for response", e);
}
}
}
private static String getFilename(AccessibleObject method)
{
PartFilename fname = method.getAnnotation(PartFilename.class);
return fname == null ? null : fname.value();
}
private static Object getFieldValue(Field field, Object obj)
{
try
{
return field.get(obj);
}
catch (IllegalAccessException e)
{
return null;
}
}
public static String getPathAnnotationValue(Class<?> clazz, String methodName, Class<?>... methodParameterTypes)
{
Method method;
try
{
method = clazz.getMethod(methodName, methodParameterTypes);
if (method.isAnnotationPresent(Path.class))
{
return method.getAnnotation(Path.class).value();
}
}
catch (NoSuchMethodException e)
{
return "";
}
return "";
}
}