/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.jira;

import java.net.URI;
import java.net.URISyntaxException;

import javax.inject.Named;

import org.xwiki.component.annotation.Component;
import org.xwiki.script.service.ScriptService;

import com.atlassian.jira.rest.client.AuthenticationHandler;
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.auth.AnonymousAuthenticationHandler;
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;

/**
 * Expose Atlassian's JIRA REST service to XWiki scripts.
 *
 * @version $Id$
 * @since 3.2M3
 */
@Component
@Named("jira")
public class JiraScriptService implements ScriptService
{
    /**
     * Note that the password will be passed in clear over the network to the remote JIRA instance. Thus, only use this
     * method when connecting over HTTPS.
     *
     * @param jiraURL the URL to the remote JIRA instance to connect to
     * @param username the username to connect to JIRA
     * @param password the password to connect to JIRA
     * @return the client to interact with the remote JIRA instance
     */
    public JiraRestClient getJiraRestClient(String jiraURL, String username, String password)
    {
        return getJiraRestClient(jiraURL, new BasicHttpAuthenticationHandler(username, password));
    }

    /**
     * Connect anonymously to the remote JIRA instance.
     *
     * @param jiraURL the URL to the remote JIRA instance to connect to
     * @return the client to interact with the remote JIRA instance
     */
    public JiraRestClient getJiraRestClient(String jiraURL)
    {
        return getJiraRestClient(jiraURL, new AnonymousAuthenticationHandler());
    }

    /**
     * @param jiraURL the URL to the remote JIRA instance to connect to
     * @param authenticationHandler the authentication to use (anonymous, basic, etc)
     * @return the client to interact with the remote JIRA instance
     */
    private JiraRestClient getJiraRestClient(String jiraURL, AuthenticationHandler authenticationHandler)
    {
        JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
        URI jiraServerUri = null;
        try {
            jiraServerUri = new URI(jiraURL);
        } catch (URISyntaxException e) {
            throw new RuntimeException("Invalid URL [" + jiraURL + "]", e);
        }
        JiraRestClient restClient = factory.create(jiraServerUri, authenticationHandler);
        return restClient;
    }
}

@@ -994,15 +994,68 @@
 ect.
-Named;%0A
+Inject;%0Aimport javax.inject.Named;%0A%0Aimport org.slf4j.Logger;
 %0Aimp
@@ -1683,16 +1683,98 @@
 rvice%0A%7B%0A
+    /**%0A     * The logger to log.%0A     */%0A    @Inject%0A    private Logger logger;%0A%0A
     /**%0A
@@ -3125,32 +3125,85 @@
 nHandler)%0A    %7B%0A
+        JiraRestClient restClient;%0A        try %7B%0A    
         JerseyJi
@@ -3275,16 +3275,20 @@
         
+    
 URI jira
@@ -3304,208 +3304,221 @@
  = n
-ull;%0A        try %7B%0A            jiraServerUri = new URI(jiraURL);%0A        %7D catch (URISyntaxException e) %7B%0A            throw new RuntimeException(%22Invalid URL %5B%22 + jiraURL + %22%5D%22, e
+ew URI(jiraURL);%0A            restClient = factory.create(jiraServerUri, authenticationHandler);%0A        %7D catch (URISyntaxException e) %7B%0A            this.logger.warn(%22Invalid JIRA URL %5B%7B%7D%5D%22, jiraURL
 );%0A    
-    %7D%0A
         
 Jira
@@ -3517,89 +3517,36 @@
     
-JiraRestClient restClient = factory.create(jiraServerUri, authenticationHandler);
+restClient = null;%0A        %7D
 %0A   

Replace xwiki-swizzle module by a xwiki module using the new JIRA REST API * Return null when the passed URL is invalid