]> git.basschouten.com Git - openhab-addons.git/blob
79d9fc3a33929ac8d4c95e48b00ef2555fdec41b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.icalendar.internal.handler;
14
15 import static org.openhab.binding.icalendar.internal.ICalendarBindingConstants.HTTP_TIMEOUT_SECS;
16
17 import java.io.File;
18 import java.io.FileInputStream;
19 import java.io.FileOutputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.URI;
23 import java.nio.file.Files;
24 import java.nio.file.StandardCopyOption;
25 import java.util.concurrent.ExecutionException;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.TimeoutException;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.eclipse.jetty.client.HttpClient;
32 import org.eclipse.jetty.client.api.Authentication;
33 import org.eclipse.jetty.client.api.Request;
34 import org.eclipse.jetty.client.api.Response;
35 import org.eclipse.jetty.client.util.BasicAuthentication;
36 import org.eclipse.jetty.client.util.InputStreamResponseListener;
37 import org.eclipse.jetty.http.HttpHeader;
38 import org.eclipse.jetty.http.HttpMethod;
39 import org.eclipse.jetty.http.HttpStatus;
40 import org.openhab.binding.icalendar.internal.logic.AbstractPresentableCalendar;
41 import org.openhab.binding.icalendar.internal.logic.CalendarException;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * The Job for pulling an update of a calendar. Fires
47  * {@link CalendarUpdateListener#onCalendarUpdated()} after successful update.
48  *
49  * @author Michael Wodniok - Initial contribution
50  * @author Michael Wodniok - Added better descriptions for some errors while
51  *         downloading calendar
52  */
53 @NonNullByDefault
54 class PullJob implements Runnable {
55     private static final String TMP_FILE_PREFIX = "icalendardld";
56
57     private final Authentication.@Nullable Result authentication;
58     private final File destination;
59     private final HttpClient httpClient;
60     private final CalendarUpdateListener listener;
61     private final Logger logger = LoggerFactory.getLogger(PullJob.class);
62     private final int maxSize;
63     private final URI sourceURI;
64
65     /**
66      * Constructor of PullJob for creating a single pull of a calendar.
67      *
68      * @param httpClient A HttpClient for getting the source
69      * @param sourceURI The source as URI
70      * @param username Optional username for basic auth. Must be set together with a password.
71      * @param password Optional password for basic auth. Must be set together with an username.
72      * @param destination The destination the downloaded calendar should be saved to.
73      * @param maxSize The maximum size of the downloaded calendar in bytes.
74      * @param listener The listener that should be fired when update succeed.
75      */
76     public PullJob(HttpClient httpClient, URI sourceURI, @Nullable String username, @Nullable String password,
77             File destination, int maxSize, CalendarUpdateListener listener) {
78         this.httpClient = httpClient;
79         this.sourceURI = sourceURI;
80         if (username != null && password != null) {
81             authentication = new BasicAuthentication.BasicResult(this.sourceURI, username, password);
82         } else {
83             authentication = null;
84         }
85         this.destination = destination;
86         this.listener = listener;
87         this.maxSize = maxSize;
88     }
89
90     @Override
91     public void run() {
92         final Request request = httpClient.newRequest(sourceURI).followRedirects(true).method(HttpMethod.GET)
93                 .timeout(HTTP_TIMEOUT_SECS, TimeUnit.SECONDS);
94         final Authentication.Result currentAuthentication = authentication;
95         if (currentAuthentication != null) {
96             currentAuthentication.apply(request);
97         }
98
99         final InputStreamResponseListener asyncListener = new InputStreamResponseListener();
100         request.send(asyncListener);
101
102         Response response;
103         try {
104             response = asyncListener.get(HTTP_TIMEOUT_SECS, TimeUnit.SECONDS);
105         } catch (InterruptedException e1) {
106             logger.warn("Download of calendar was interrupted: {}", e1.getMessage());
107             request.abort(e1.getCause() != null ? e1.getCause() : e1);
108             return;
109         } catch (TimeoutException e1) {
110             logger.warn("Download of calendar timed out (waited too long for headers): {}", e1.getMessage());
111             request.abort(e1.getCause() != null ? e1.getCause() : e1);
112             return;
113         } catch (ExecutionException e1) {
114             String msg = e1.getCause() != null ? e1.getCause().getMessage() : "";
115             logger.warn("Download of calendar failed with ExecutionException: {}", msg);
116             request.abort(e1.getCause() != null ? e1.getCause() : e1);
117             return;
118         }
119
120         if (response.getStatus() != HttpStatus.OK_200) {
121             logger.warn("Response status for getting \"{}\" was {} instead of 200. Ignoring it.", sourceURI,
122                     response.getStatus());
123             request.abort(new IllegalStateException(
124                     "Got response status " + response.getStatus() + " while requesting " + sourceURI));
125             return;
126         }
127
128         final String responseLength = response.getHeaders().get(HttpHeader.CONTENT_LENGTH);
129         if (responseLength != null) {
130             try {
131                 if (Integer.parseInt(responseLength) > maxSize) {
132                     logger.warn(
133                             "Calendar is too big ({} bytes > {} bytes), aborting request. You may change the maximum calendar size in configuration, if appropriate.",
134                             responseLength, maxSize);
135                     response.abort(new ResponseTooBigException());
136                     return;
137                 }
138             } catch (NumberFormatException e) {
139                 logger.debug(
140                         "While requesting calendar Content-Length was set, but is malformed. Falling back to read-loop.",
141                         e);
142             }
143         }
144
145         File tmpTargetFile;
146         try {
147             tmpTargetFile = Files.createTempFile(TMP_FILE_PREFIX, null).toFile();
148         } catch (IOException e) {
149             logger.warn("Not able to create temporary file for downloading iCal. Error message is: {}", e.getMessage());
150             return;
151         }
152
153         try (final FileOutputStream tmpOutStream = new FileOutputStream(tmpTargetFile);
154                 final InputStream httpInputStream = asyncListener.getInputStream()) {
155             final byte[] buffer = new byte[1024];
156             int readBytesTotal = 0;
157             int currentReadBytes = -1;
158             while ((currentReadBytes = httpInputStream.read(buffer)) > -1) {
159                 readBytesTotal += currentReadBytes;
160                 if (readBytesTotal > maxSize) {
161                     logger.warn(
162                             "Calendar is too big (> {} bytes). Stopping receiving calendar. You may change the maximum calendar size in configuration, if appropriate.",
163                             maxSize);
164                     response.abort(new ResponseTooBigException());
165                     return;
166                 }
167                 tmpOutStream.write(buffer, 0, currentReadBytes);
168             }
169         } catch (IOException e) {
170             logger.warn("Not able to write temporary file with downloaded iCal. Error Message is: {}", e.getMessage());
171             return;
172         }
173
174         try (final FileInputStream tmpInput = new FileInputStream(tmpTargetFile)) {
175             AbstractPresentableCalendar.create(tmpInput);
176         } catch (IOException | CalendarException e) {
177             logger.warn(
178                     "Not able to read downloaded iCal. Validation failed or file not readable. Error message is: {}",
179                     e.getMessage());
180             return;
181         }
182
183         try {
184             Files.move(tmpTargetFile.toPath(), destination.toPath(), StandardCopyOption.REPLACE_EXISTING);
185         } catch (IOException e) {
186             logger.warn("Failed to replace iCal file. Error message is: {}", e.getMessage());
187             return;
188         }
189
190         try {
191             listener.onCalendarUpdated();
192         } catch (Exception e) {
193             logger.debug("An Exception was thrown while calling back", e);
194         }
195     }
196
197     /**
198      * Interface for calling back when the update succeed.
199      */
200     public static interface CalendarUpdateListener {
201         /**
202          * Callback when update was successful and result was placed onto target file.
203          */
204         public void onCalendarUpdated();
205     }
206
207     /**
208      * Exception for failure if size of the response is greater than allowed.
209      */
210     private static class ResponseTooBigException extends Exception {
211
212         /**
213          * The only local definition. Rest of implementation is taken from Exception or is default.
214          */
215         private static final long serialVersionUID = 7033851403473533793L;
216     }
217 }