]> git.basschouten.com Git - openhab-addons.git/commitdiff
Add useragent parameter (#17455)
authorlsiepel <leosiepel@gmail.com>
Sat, 5 Oct 2024 07:27:20 +0000 (09:27 +0200)
committerGitHub <noreply@github.com>
Sat, 5 Oct 2024 07:27:20 +0000 (09:27 +0200)
Signed-off-by: Leo Siepel <leosiepel@gmail.com>
bundles/org.openhab.binding.icalendar/README.md
bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/config/ICalendarConfiguration.java
bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/ICalendarHandler.java
bundles/org.openhab.binding.icalendar/src/main/java/org/openhab/binding/icalendar/internal/handler/PullJob.java
bundles/org.openhab.binding.icalendar/src/main/resources/OH-INF/i18n/icalendar.properties
bundles/org.openhab.binding.icalendar/src/main/resources/OH-INF/thing/thing-types.xml

index 150dd34df5b816ad46e91cabece644d8e4449d06..f355cb9f4e0a989566865670a1621fc513a0f966 100644 (file)
@@ -26,6 +26,7 @@ Each `calendar` thing requires the following configuration parameters:
 | `password`          | The password for pulling the calendar. If set, the binding pulls the calendar using basic auth. Only valid in combination with `username`.                                                | optional                      |
 | `maxSize`           | The maximum size of the iCal-file in Mebibytes.                                                                                                                                           | mandatory (default available) |
 | `authorizationCode` | The authorization code to permit the execution of embedded command tags. If set, the binding checks that the authorization code in the command tag matches before executing any commands. | optional                      |
+| `userAgent`         | Some providers require a specific user agent header. If left empty, the default Jetty header is used.                                                                                     | optional                      |
 
 ### Configuration for `eventfilter`
 
index 53b3f9236ac9f28c9cb4edc1b15ec6ab1f890706..28011739b312d8acdda58d0d293712fc34c89a94 100644 (file)
@@ -153,7 +153,8 @@ public class ICalendarHandler extends BaseBridgeHandler implements CalendarUpdat
             final int maxSize = maxSizeBD.intValue();
             try {
                 regularPull = new PullJob(httpClient, new URI(currentConfiguration.url), currentConfiguration.username,
-                        currentConfiguration.password, calendarFile, maxSize * 1048576, this);
+                        currentConfiguration.password, calendarFile, maxSize * 1048576, this,
+                        currentConfiguration.userAgent);
             } catch (URISyntaxException e) {
                 throw new ConfigBrokenException(String.format(
                         "The URI '%s' for downloading the calendar contains syntax errors.", currentConfiguration.url));
index 79d9fc3a33929ac8d4c95e48b00ef2555fdec41b..112d5025e4c31f9e6f43a6a6f7669fb4bb865236 100644 (file)
@@ -61,6 +61,7 @@ class PullJob implements Runnable {
     private final Logger logger = LoggerFactory.getLogger(PullJob.class);
     private final int maxSize;
     private final URI sourceURI;
+    private @Nullable final String userAgent;
 
     /**
      * Constructor of PullJob for creating a single pull of a calendar.
@@ -74,7 +75,7 @@ class PullJob implements Runnable {
      * @param listener The listener that should be fired when update succeed.
      */
     public PullJob(HttpClient httpClient, URI sourceURI, @Nullable String username, @Nullable String password,
-            File destination, int maxSize, CalendarUpdateListener listener) {
+            File destination, int maxSize, CalendarUpdateListener listener, @Nullable String userAgent) {
         this.httpClient = httpClient;
         this.sourceURI = sourceURI;
         if (username != null && password != null) {
@@ -85,12 +86,16 @@ class PullJob implements Runnable {
         this.destination = destination;
         this.listener = listener;
         this.maxSize = maxSize;
+        this.userAgent = userAgent;
     }
 
     @Override
     public void run() {
         final Request request = httpClient.newRequest(sourceURI).followRedirects(true).method(HttpMethod.GET)
                 .timeout(HTTP_TIMEOUT_SECS, TimeUnit.SECONDS);
+        if (userAgent != null && !userAgent.isBlank()) {
+            request.agent(userAgent);
+        }
         final Authentication.Result currentAuthentication = authentication;
         if (currentAuthentication != null) {
             currentAuthentication.apply(request);
index f0013970c67b7e6140d24bc997965744b595c917..34675a0924a89f49c896c4ec784f3f245ce0b0d4 100644 (file)
@@ -24,6 +24,8 @@ thing-type.config.icalendar.calendar.refreshTime.label = Refresh Time
 thing-type.config.icalendar.calendar.refreshTime.description = Frequency to scan for changes in minutes
 thing-type.config.icalendar.calendar.url.label = URL
 thing-type.config.icalendar.calendar.url.description = URL for downloading iCalendar events
+thing-type.config.icalendar.calendar.userAgent.label = User Agent
+thing-type.config.icalendar.calendar.userAgent.description = Some providers require a specific user agent header. If left empty, the default Jetty header is used.
 thing-type.config.icalendar.calendar.username.label = User Name
 thing-type.config.icalendar.calendar.username.description = User name for fetching the calendar (usable in combination with password in HTTP basic auth)
 thing-type.config.icalendar.eventfilter.datetimeEnd.label = End
index 756d6c3e36efc6051a69fb7f93608b1352ec1b8d..f7fbdd86cf8b77ec03efb7eda4af66cf45578907 100644 (file)
                                <label>Command Authorization Code</label>
                                <description>Authorization Code to allow the execution of Command Tags (may be empty)</description>
                        </parameter>
+                       <parameter name="userAgent" type="text" required="false">
+                               <label>User Agent</label>
+                               <description>Some providers require a specific user agent header. If left empty, the default Jetty header is used.</description>
+                               <advanced>true</advanced>
+                       </parameter>
                </config-description>
 
        </bridge-type>