3232import org .phoebus .util .http .HttpRequestMultipartBody ;
3333import org .phoebus .util .http .QueryParamsHelper ;
3434
35+ import javax .ws .rs .HttpMethod ;
3536import javax .ws .rs .core .MultivaluedHashMap ;
3637import javax .ws .rs .core .MultivaluedMap ;
3738import java .io .InputStream ;
@@ -158,7 +159,7 @@ private OlogHttpClient(String userName, String password) {
158159
159160 @ Override
160161 public LogEntry set (LogEntry log ) throws LogbookException {
161- return save (log , null );
162+ return save (log , null , HttpMethod . PUT );
162163 }
163164
164165 /**
@@ -171,7 +172,7 @@ public LogEntry set(LogEntry log) throws LogbookException {
171172 * @throws LogbookException E.g. due to invalid log entry data, or if attachment content type
172173 * cannot be determined.
173174 */
174- private LogEntry save (LogEntry log , LogEntry inReplyTo ) throws LogbookException {
175+ private LogEntry save (LogEntry log , LogEntry inReplyTo , String method ) throws LogbookException {
175176 try {
176177 javax .ws .rs .core .MultivaluedMap <String , String > queryParams = new javax .ws .rs .core .MultivaluedHashMap <>();
177178 queryParams .putSingle ("markup" , "commonmark" );
@@ -183,24 +184,29 @@ private LogEntry save(LogEntry log, LogEntry inReplyTo) throws LogbookException
183184 httpRequestMultipartBody .addTextPart ("logEntry" , OlogObjectMappers .logEntrySerializer .writeValueAsString (log ), "application/json" );
184185
185186 for (Attachment attachment : log .getAttachments ()) {
186- httpRequestMultipartBody .addFilePart (attachment .getFile (), attachment .getUniqueFilename ());
187+ if (attachment .getFile () != null ){
188+ httpRequestMultipartBody .addFilePart (attachment .getFile (), attachment .getUniqueFilename ());
189+ }
187190 }
188191
192+ HttpRequest .BodyPublisher bodyPublisher = HttpRequest .BodyPublishers .ofByteArray (httpRequestMultipartBody .getBytes ());
193+
189194 HttpRequest request = HttpRequest .newBuilder ()
190195 .uri (URI .create (Preferences .olog_url + OLOG_PREFIX + "/logs/multipart?" + QueryParamsHelper .mapToQueryParams (queryParams )))
191196 .header ("Content-Type" , httpRequestMultipartBody .getContentType ())
192197 .header (OLOG_CLIENT_INFO_HEADER , CLIENT_INFO )
193198 .header ("Authorization" , basicAuthenticationHeader )
194- .PUT (HttpRequest .BodyPublishers .ofByteArray (httpRequestMultipartBody .getBytes ()))
199+ .method (method , bodyPublisher )
200+ //.PUT(HttpRequest.BodyPublishers.ofByteArray(httpRequestMultipartBody.getBytes()))
195201 .build ();
196202
197203 HttpResponse <String > response = httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
198204 if (response .statusCode () == 401 ){
199- LOGGER .log (Level .SEVERE , "Failed to create log entry: user not authenticated" );
205+ LOGGER .log (Level .SEVERE , "Failed to create or update log entry: user not authenticated" );
200206 throw new LogbookException (Messages .SubmissionFailedInvalidCredentials );
201207 }
202208 else if (response .statusCode () >= 300 ) {
203- LOGGER .log (Level .SEVERE , "Failed to create log entry: " + response .body ());
209+ LOGGER .log (Level .SEVERE , "Failed to create or update log entry: " + response .body ());
204210 throw new LogbookException (response .body ());
205211 } else {
206212 return OlogObjectMappers .logEntryDeserializer .readValue (response .body (), OlogLog .class );
@@ -213,7 +219,7 @@ else if (response.statusCode() >= 300) {
213219
214220 @ Override
215221 public LogEntry reply (LogEntry log , LogEntry inReplyTo ) throws LogbookException {
216- return save (log , inReplyTo );
222+ return save (log , inReplyTo , HttpMethod . PUT );
217223 }
218224
219225 /**
@@ -305,32 +311,21 @@ public String getServiceUrl() {
305311 }
306312
307313 /**
308- * Updates an existing {@link LogEntry}. Note that unlike the {@link #save(LogEntry, LogEntry)} API,
309- * this does not support attachments, i.e. it does not set up a multipart request to the service.
314+ * Updates an existing {@link LogEntry}.
315+ * <p>NOTE:</p>
316+ * The list of attachments in the {@link LogEntry} may contain additional attachments added by the user. Moreover,
317+ * attachments listed in the {@link LogEntry} subject to change, but not listed in the {@link LogEntry} submitted
318+ * to the service, will be removed. This makes it possible for a user to update an entry such that a
319+ * potentially erroneous attachment is replaced by a correct one.
310320 *
311321 * @param logEntry - the updated log entry
312322 * @return The updated {@link LogEntry}
313323 */
314324 @ Override
315- public LogEntry update (LogEntry logEntry ) {
316-
317- try {
318- HttpRequest request = HttpRequest .newBuilder ()
319- .uri (URI .create (Preferences .olog_url + OLOG_PREFIX + "/logs/" + logEntry .getId () + "?markup=commonmark" ))
320- .header ("Content-Type" , CONTENT_TYPE_JSON )
321- .header ("Authorization" , basicAuthenticationHeader )
322- .POST (HttpRequest .BodyPublishers .ofString (OlogObjectMappers .logEntrySerializer .writeValueAsString (logEntry )))
323- .build ();
324-
325- HttpResponse <String > response = httpClient .send (request , HttpResponse .BodyHandlers .ofString ());
326-
327- LogEntry updated = OlogObjectMappers .logEntryDeserializer .readValue (response .body (), OlogLog .class );
328- changeHandlers .forEach (h -> h .logEntryChanged (updated ));
329- return updated ;
330- } catch (Exception e ) {
331- LOGGER .log (Level .SEVERE , "Unable to update log entry id=" + logEntry .getId (), e );
332- return null ;
333- }
325+ public LogEntry update (LogEntry logEntry ) throws LogbookException {
326+ LogEntry updated = save (logEntry , null , HttpMethod .POST );
327+ changeHandlers .forEach (h -> h .logEntryChanged (updated ));
328+ return updated ;
334329 }
335330
336331 @ Override
@@ -502,6 +497,25 @@ public InputStream getAttachment(Long logId, String attachmentName) {
502497 }
503498 }
504499
500+ @ Override
501+ public InputStream getAttachment (Long logId , Attachment attachment ){
502+ try {
503+ HttpRequest request = HttpRequest .newBuilder ()
504+ .uri (URI .create (Preferences .olog_url + OLOG_PREFIX + "/attachment/" + attachment .getId ()))
505+ .GET ()
506+ .build ();
507+ HttpResponse <InputStream > response = httpClient .send (request , HttpResponse .BodyHandlers .ofInputStream ());
508+ if (response .statusCode () >= 300 ) {
509+ LOGGER .log (Level .WARNING , "failed to obtain attachment: " + new String (response .body ().readAllBytes ()));
510+ return null ;
511+ }
512+ return response .body ();
513+ } catch (Exception e ) {
514+ LOGGER .log (Level .WARNING , "failed to obtain attachment" , e );
515+ return null ;
516+ }
517+ }
518+
505519 /**
506520 * @param id Unique log entry id
507521 * @return A {@link SearchResult} containing a list of {@link LogEntry} objects representing the
0 commit comments