Skip to content
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

Update change logo endpoint to POST with image as form data #4311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3045,11 +3045,11 @@ void deleteLabel(@HeaderParam("sessionid") String sessionId, @PathParam("labelId
*
* @param sessionId
* current session
* @param imageData
* PNG or JPEG image as a byte array
* @param multipart
* PNG or Baseline JPEG image
*/
@PUT
@POST
@Path("logo")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
void updateLogo(@HeaderParam("sessionid") String sessionId, byte[] imageData) throws RestException;
@Consumes(MediaType.MULTIPART_FORM_DATA)
void updateLogo(@HeaderParam("sessionid") String sessionId, MultipartFormDataInput multipart) throws RestException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,35 @@ public boolean pushFile(String sessionId, String space, String path, String file
return response.readEntity(Boolean.class);
}

public void updateLogo(String sessionId, InputStream fileContent) throws NotConnectedRestException {

String uriTmpl = (new StringBuilder(restEndpointURL)).append(addSlashIfMissing(restEndpointURL))
.append("scheduler/logo/")
.toString();

ResteasyClient client = buildResteasyClient(providerFactory);

ResteasyWebTarget target = client.target(uriTmpl);

MultipartFormDataOutput formData = new MultipartFormDataOutput();
formData.addFormData("fileContent", fileContent, MediaType.APPLICATION_OCTET_STREAM_TYPE);

GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(formData) {
};

Response response = target.request()
.header("sessionid", sessionId)
.post(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));

if (response.getStatus() != HttpURLConnection.HTTP_OK) {
if (response.getStatus() == HttpURLConnection.HTTP_UNAUTHORIZED) {
throw new NotConnectedRestException("User not authenticated or session timeout.");
} else {
throwException(String.format("Update logo failed. Status code: %d", response.getStatus()), response);
}
}
}

public void pullFile(String sessionId, String space, String path, String outputPath) throws Exception {
String uriTmpl = (new StringBuilder(restEndpointURL)).append(addSlashIfMissing(restEndpointURL))
.append("scheduler/dataspace/")
Expand Down Expand Up @@ -787,6 +816,5 @@ public void write(OutputStream outputStream) throws IOException, WebApplicationE
}

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@
import static org.ow2.proactive.scheduler.rest.data.DataUtility.toSchedulerUserInfos;
import static org.ow2.proactive.scheduler.rest.data.DataUtility.toTaskResult;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.io.*;
import java.lang.reflect.Proxy;
import java.net.URL;
import java.net.URLConnection;
Expand Down Expand Up @@ -79,6 +75,7 @@
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.log4j.Logger;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.objectweb.proactive.utils.StackTraceUtil;
import org.ow2.proactive.authentication.ConnectionInfo;
Expand Down Expand Up @@ -2039,8 +2036,8 @@ public void removeJobLabels(List<String> jobIds) throws NotConnectedException, P

@Override
public void updateLogo(byte[] image) throws NotConnectedException, PermissionException {
try {
restApi().updateLogo(sid, image);
try (ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(image)) {
restApiClient().updateLogo(sid, byteArrayInputStream);
} catch (Exception e) {
throwNCEOrPE(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.db.SortOrder;
import org.ow2.proactive.db.SortParameter;
import org.ow2.proactive.permissions.RoleAdmin;
import org.ow2.proactive.permissions.RoleRead;
import org.ow2.proactive.permissions.RoleWrite;
import org.ow2.proactive.scheduler.common.*;
Expand Down Expand Up @@ -3703,12 +3704,26 @@ public void deleteLabel(String sessionId, final String labelId) throws RestExcep
}

@Override
public void updateLogo(String sessionId, byte[] imageData) throws RestException {
@RoleAdmin
public void updateLogo(String sessionId, MultipartFormDataInput multipart) throws RestException {
Scheduler scheduler = checkAccess(sessionId);
try {
Scheduler scheduler = checkAccess(sessionId);
scheduler.updateLogo(imageData);
} catch (SchedulerException e) {
throw RestException.wrapExceptionToRest(e);
Map<String, List<InputPart>> formDataMap = multipart.getFormDataMap();

List<InputPart> fCL = formDataMap.get("fileContent");
if ((fCL == null) || (fCL.isEmpty())) {
throw new IllegalArgumentException("Illegal multipart argument definition (fileContent), received " +
fCL);
}

InputStream fileContent = fCL.get(0).getBody(InputStream.class, null);
scheduler.updateLogo(IOUtils.toByteArray(fileContent));
} catch (SchedulerException | IOException e) {
throw RestException.wrapExceptionToRest(new SchedulerException(e));
} finally {
if (multipart != null) {
multipart.close();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3003,7 +3003,7 @@ public void updateLogo(byte[] image) throws NotConnectedException, PermissionExc
"You don't have permissions to update a new logo");
try {
schedulingService.updateLogo(image, ident.getUsername());
} catch (Exception e) {
} catch (IOException e) {
throw new ImageValidationException(String.format("Failed to update logo uploaded by %s ",
ident.getUsername()),
e);
Expand Down