Author: jmssiera
Date: Tue Jul 19 13:27:31 2022
New Revision: 1902854

URL: http://svn.apache.org/viewvc?rev=1902854&view=rev
Log:
CONNECTORS-1721: Confluence v6 does not distinguish 404 errors

Modified:
    
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/ConfluenceRepositoryConnector.java
    
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/client/ConfluenceClient.java

Modified: 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/ConfluenceRepositoryConnector.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/ConfluenceRepositoryConnector.java?rev=1902854&r1=1902853&r2=1902854&view=diff
==============================================================================
--- 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/ConfluenceRepositoryConnector.java
 (original)
+++ 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/ConfluenceRepositoryConnector.java
 Tue Jul 19 13:27:31 2022
@@ -524,8 +524,8 @@ public class ConfluenceRepositoryConnect
   /*
    * Repository specification post handle, (server and proxy & client secret 
etc)
    *
-   * @see 
org.apache.manifoldcf.core.connector.BaseConnector#processConfigurationPost 
(org.apache.manifoldcf.core.interfaces.IThreadContext,
-   * org.apache.manifoldcf.core.interfaces.IPostParameters, 
org.apache.manifoldcf.core.interfaces.ConfigParams)
+   * @see 
org.apache.manifoldcf.core.connector.BaseConnector#processConfigurationPost 
(org.apache.manifoldcf.core.interfaces.IThreadContext, 
org.apache.manifoldcf.core.interfaces.IPostParameters,
+   * org.apache.manifoldcf.core.interfaces.ConfigParams)
    */
   @Override
   public String processConfigurationPost(final IThreadContext threadContext, 
final IPostParameters variableContext, final ConfigParams parameters) throws 
ManifoldCFException {
@@ -802,8 +802,8 @@ public class ConfluenceRepositoryConnect
     try {
 
       /*
-       * Not uses delta seeding because Confluence can't be queried using 
dates or in a ordered way, only start and limit which can cause problems if an 
already indexed document is
-       * deleted, because we will miss some to-be indexed docs due to the last 
start parameter stored in the last execution
+       * Not uses delta seeding because Confluence can't be queried using 
dates or in a ordered way, only start and limit which can cause problems if an 
already indexed document is deleted, because we
+       * will miss some to-be indexed docs due to the last start parameter 
stored in the last execution
        */
       // if(lastSeedVersion != null && !lastSeedVersion.isEmpty()) {
       // StringTokenizer tokenizer = new
@@ -847,16 +847,20 @@ public class ConfluenceRepositoryConnect
       do {
         final ConfluenceResponse<Page> response = 
confluenceClient.getPageChilds((int) lastStart, (int) defaultSize, pageId);
 
-        int count = 0;
-        for (final Page page : response.getResults()) {
-          pageChilds.add(page);
-          count++;
-        }
+        if (response != null) {
+          int count = 0;
+          for (final Page page : response.getResults()) {
+            pageChilds.add(page);
+            count++;
+          }
 
-        lastStart += count;
-        isLast = response.isLast();
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getPageChilds" }));
+          lastStart += count;
+          isLast = response.isLast();
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getPageChilds" }));
+          }
+        } else {
+          break;
         }
       } while (!isLast);
 
@@ -881,16 +885,20 @@ public class ConfluenceRepositoryConnect
       do {
         final ConfluenceRestrictionsResponse<Restrictions> response = 
confluenceClient.getPageReadRestrictions((int) lastStart, (int) defaultSize, 
pageId);
 
-        if (response.getResult() != null) {
-          restrictionsList.add(response.getResult());
-        }
+        if (response != null) {
+          if (response.getResult() != null) {
+            restrictionsList.add(response.getResult());
+          }
 
-        isLast = response.isLast();
-        if (!isLast) {
-          lastStart += defaultSize;
-        }
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getAllSpaceKeys" }));
+          isLast = response.isLast();
+          if (!isLast) {
+            lastStart += defaultSize;
+          }
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getAllSpaceKeys" }));
+          }
+        } else {
+          break;
         }
       } while (!isLast);
 
@@ -914,16 +922,20 @@ public class ConfluenceRepositoryConnect
       do {
         final ConfluenceResponse<Space> response = 
confluenceClient.getSpaces((int) lastStart, (int) defaultSize, 
Optional.<String>absent(), Optional.<String>absent());
 
-        int count = 0;
-        for (final Space space : response.getResults()) {
-          spaceKeys.add(space.getKey());
-          count++;
-        }
+        if (response != null) {
+          int count = 0;
+          for (final Space space : response.getResults()) {
+            spaceKeys.add(space.getKey());
+            count++;
+          }
 
-        lastStart += count;
-        isLast = response.isLast();
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getAllSpaceKeys" }));
+          lastStart += count;
+          isLast = response.isLast();
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getAllSpaceKeys" }));
+          }
+        } else {
+          break;
         }
       } while (!isLast);
     } catch (final Exception e) {
@@ -958,23 +970,27 @@ public class ConfluenceRepositoryConnect
 //        final ConfluenceResponse<Page> response = confluenceClient.getPages(
 //            (int) lastStart, (int) defaultSize, space, pageType);
 
-        int count = 0;
-        for (final Page page : response.getResults()) {
-
-          activities.addSeedDocument(page.getId());
-          if (confluenceSpec.isProcessAttachments()) {
-            processSeedAttachments(page, activities);
+        if (response != null) {
+          int count = 0;
+          for (final Page page : response.getResults()) {
+
+            activities.addSeedDocument(page.getId());
+            if (confluenceSpec.isProcessAttachments()) {
+              processSeedAttachments(page, activities);
+            }
+            count++;
+          }
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("Fetched and added {0} 
seed documents", Locale.ROOT).format(new Object[] { new Integer(count) }));
           }
-          count++;
-        }
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("Fetched and added {0} 
seed documents", Locale.ROOT).format(new Object[] { new Integer(count) }));
-        }
 
-        lastStart += count;
-        isLast = response.isLast();
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+          lastStart += count;
+          isLast = response.isLast();
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+          }
+        } else {
+          break;
         }
       } while (!isLast);
 
@@ -1005,20 +1021,24 @@ public class ConfluenceRepositoryConnect
       do {
         final ConfluenceResponse<Attachment> response = 
confluenceClient.getPageAttachments(page.getId(), (int) lastStart, (int) 
defaultSize);
 
-        int count = 0;
-        for (final Page resultPage : response.getResults()) {
-          
activities.addSeedDocument(ConfluenceUtil.generateRepositoryDocumentIdentifier(resultPage.getId(),
 page.getId()));
-          count++;
-        }
+        if (response != null) {
+          int count = 0;
+          for (final Page resultPage : response.getResults()) {
+            
activities.addSeedDocument(ConfluenceUtil.generateRepositoryDocumentIdentifier(resultPage.getId(),
 page.getId()));
+            count++;
+          }
 
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("Fetched and added {} 
seed document attachments for page {}", Locale.ROOT).format(new Object[] { new 
Integer(count), page.getId() }));
-        }
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("Fetched and added {} 
seed document attachments for page {}", Locale.ROOT).format(new Object[] { new 
Integer(count), page.getId() }));
+          }
 
-        lastStart += count;
-        isLast = response.isLast();
-        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
-          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+          lastStart += count;
+          isLast = response.isLast();
+          if (Logging.connectors != null && 
Logging.connectors.isDebugEnabled()) {
+            Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+          }
+        } else {
+          break;
         }
       } while (!isLast);
 
@@ -1056,8 +1076,8 @@ public class ConfluenceRepositoryConnect
   /*
    * Process documents
    *
-   * @see org.apache.manifoldcf.crawler.connectors.BaseRepositoryConnector# 
processDocuments(java.lang.String[], java.lang.String[],
-   * org.apache.manifoldcf.crawler.interfaces.IProcessActivity, 
org.apache.manifoldcf.crawler.interfaces.DocumentSpecification, boolean[])
+   * @see org.apache.manifoldcf.crawler.connectors.BaseRepositoryConnector# 
processDocuments(java.lang.String[], java.lang.String[], 
org.apache.manifoldcf.crawler.interfaces.IProcessActivity,
+   * org.apache.manifoldcf.crawler.interfaces.DocumentSpecification, boolean[])
    */
   @Override
   public void processDocuments(final String[] documentIdentifiers, final 
IExistingVersions statuses, final Specification spec, final IProcessActivity 
activities, final int jobMode,
@@ -1158,7 +1178,11 @@ public class ConfluenceRepositoryConnect
     } catch (final Exception e) {
       handlePageException(e, "page processing");
     }
-    return processPageInternal(activeSecurity, parentRestrictions, page, 
documentIdentifier, version, activities, doLog, extraProperties);
+    if (page != null) {
+      return processPageInternal(activeSecurity, parentRestrictions, page, 
documentIdentifier, version, activities, doLog, extraProperties);
+    } else {
+      return null;
+    }
 
   }
 
@@ -1223,8 +1247,8 @@ public class ConfluenceRepositoryConnect
     final DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, 
DateFormat.MEDIUM, Locale.ROOT);
 
     /*
-     * Retain page in Manifold because it has not changed from last time This 
is needed to keep the identifier in Manifold data, because by default if a 
document is not retained nor
-     * ingested, it will be deleted by the framework
+     * Retain page in Manifold because it has not changed from last time This 
is needed to keep the identifier in Manifold data, because by default if a 
document is not retained nor ingested, it will
+     * be deleted by the framework
      */
     final StringBuilder versionBuilder = new StringBuilder();
     versionBuilder.append(df.format(lastModified));

Modified: 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/client/ConfluenceClient.java
URL: 
http://svn.apache.org/viewvc/manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/client/ConfluenceClient.java?rev=1902854&r1=1902853&r2=1902854&view=diff
==============================================================================
--- 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/client/ConfluenceClient.java
 (original)
+++ 
manifoldcf/trunk/connectors/confluence-v6/connector/src/main/java/org/apache/manifoldcf/crawler/connectors/confluence/v6/client/ConfluenceClient.java
 Tue Jul 19 13:27:31 2022
@@ -85,8 +85,8 @@ import com.google.common.collect.Lists;
  * This class is intended to be used to interact with Confluence REST API
  * </p>
  * <p>
- * There are some methods that make use of the Confluence JSON-RPC 2.0 API, 
but until all the methods are ported to the new REST API, we
- * will have to use them to leverage all the features provided by Confluence
+ * There are some methods that make use of the Confluence JSON-RPC 2.0 API, 
but until all the methods are ported to the new REST API, we will have to use 
them to leverage all the features provided by
+ * Confluence
  * </p>
  *
  * @author Julien Massiera &amp; Antonio David Perez Morales;
@@ -126,19 +126,13 @@ public class ConfluenceClient {
    * <p>
    * Creates a new client instance using the given parameters
    * </p>
-   * 
-   * @param protocol
-   *          the protocol
-   * @param host
-   *          the host
-   * @param port
-   *          the port
-   * @param path
-   *          the path to Confluence instance
-   * @param username
-   *          the username used to make the requests. Null or empty to use 
anonymous user
-   * @param password
-   *          the password
+   *
+   * @param protocol the protocol
+   * @param host     the host
+   * @param port     the port
+   * @param path     the path to Confluence instance
+   * @param username the username used to make the requests. Null or empty to 
use anonymous user
+   * @param password the password
    * @throws ManifoldCFException
    */
   public ConfluenceClient(final String protocol, final String host, final 
Integer port, final String path, final String username, final String password, 
final int socketTimeout,
@@ -159,7 +153,7 @@ public class ConfluenceClient {
    * <p>
    * Connect methods used to initialize the underlying client
    * </p>
-   * 
+   *
    * @throws ManifoldCFException
    */
   private void connect() throws ManifoldCFException {
@@ -258,8 +252,7 @@ public class ConfluenceClient {
    * Create a get request for the given url
    * </p>
    *
-   * @param url
-   *          the url
+   * @param url the url
    * @return the created {@code HttpGet} instance
    */
   private HttpGet createGetRequest(final String url) {
@@ -303,10 +296,8 @@ public class ConfluenceClient {
    * Get a list of Confluence pages using pagination
    * </p>
    *
-   * @param start
-   *          The start value to get pages from
-   * @param limit
-   *          The number of pages to get from start
+   * @param start The start value to get pages from
+   * @param limit The number of pages to get from start
    * @return a {@code ConfluenceResponse} containing the result pages and some 
pagination values
    * @throws Exception
    */
@@ -326,11 +317,9 @@ public class ConfluenceClient {
    * <p>
    * Get the {@code ConfluenceResources} from the given url
    * </p>
-   * 
-   * @param url
-   *          The url identifying the REST resource to get the documents
-   * @param builder
-   *          The builder used to build the resources contained in the response
+   *
+   * @param url     The url identifying the REST resource to get the documents
+   * @param builder The builder used to build the resources contained in the 
response
    * @return a {@code ConfluenceResponse} containing the page results
    * @throws Exception
    */
@@ -339,6 +328,15 @@ public class ConfluenceClient {
 
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        if (response.getStatusLine().getStatusCode() != 404) {
+          throw new Exception("Confluence error. " + errorDesc);
+        } else {
+          logger.error("[Processing] Failed to get page {}. Error: {}", url, 
errorDesc);
+          return null;
+        }
+      }
       final ConfluenceResponse<? extends ConfluenceResource> 
confluenceResponse = responseFromHttpEntity(response.getEntity(), builder);
       EntityUtils.consume(response.getEntity());
       return confluenceResponse;
@@ -352,11 +350,9 @@ public class ConfluenceClient {
    * <p>
    * Get the {@code ConfluenceResources} from the given url
    * </p>
-   * 
-   * @param url
-   *          The url identifying the REST resource to get the documents
-   * @param builder
-   *          The builder used to build the resources contained in the response
+   *
+   * @param url     The url identifying the REST resource to get the documents
+   * @param builder The builder used to build the resources contained in the 
response
    * @return a {@code ConfluenceRestrictionsResponse} containing the page 
results
    * @throws Exception
    */
@@ -366,6 +362,15 @@ public class ConfluenceClient {
 
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        if (response.getStatusLine().getStatusCode() != 404) {
+          throw new Exception("Confluence error. " + errorDesc);
+        } else {
+          logger.error("[Processing] Failed to get page {}. Error: {}", url, 
errorDesc);
+          return null;
+        }
+      }
       final ConfluenceRestrictionsResponse<? extends ConfluenceResource> 
confluenceResponse = restrictionsResponseFromHttpEntity(response.getEntity(), 
builder);
       EntityUtils.consume(response.getEntity());
       return confluenceResponse;
@@ -379,9 +384,8 @@ public class ConfluenceClient {
    * <p>
    * Creates a ConfluenceResponse from the entity returned in the HttpResponse
    * </p>
-   * 
-   * @param entity
-   *          the {@code HttpEntity} to extract the response from
+   *
+   * @param entity the {@code HttpEntity} to extract the response from
    * @return a {@code ConfluenceResponse} with the requested information
    * @throws Exception
    */
@@ -402,9 +406,8 @@ public class ConfluenceClient {
    * <p>
    * Creates a ConfluenceResponse from the entity returned in the HttpResponse
    * </p>
-   * 
-   * @param entity
-   *          the {@code HttpEntity} to extract the response from
+   *
+   * @param entity the {@code HttpEntity} to extract the response from
    * @return a {@code ConfluenceResponse} with the requested information
    * @throws Exception
    */
@@ -425,9 +428,8 @@ public class ConfluenceClient {
    * <p>
    * Get the attachments of the given page
    * </p>
-   * 
-   * @param pageId
-   *          the page id
+   *
+   * @param pageId the page id
    * @return a {@code ConfluenceResponse} instance containing the attachment 
results and some pagination values
    * @throws Exception
    */
@@ -439,13 +441,10 @@ public class ConfluenceClient {
    * <p>
    * Get the attachments of the given page using pagination
    * </p>
-   * 
-   * @param pageId
-   *          the page id
-   * @param start
-   *          The start value to get attachments from
-   * @param limit
-   *          The number of attachments to get from start
+   *
+   * @param pageId the page id
+   * @param start  The start value to get attachments from
+   * @param limit  The number of attachments to get from start
    * @return a {@code ConfluenceResponse} instance containing the attachment 
results and some pagination values
    * @throws Exception
    */
@@ -470,6 +469,11 @@ public class ConfluenceClient {
     logger.debug("[Processing] Hitting url for getting document content : {}", 
sanitizeUrl(url));
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        response.close();
+        throw new Exception("Confluence error. " + errorDesc);
+      }
       final HttpEntity entity = response.getEntity();
       final MutableAttachment attachment = attachmentFromHttpEntity(entity);
       EntityUtils.consume(entity);
@@ -486,8 +490,7 @@ public class ConfluenceClient {
    * Downloads and retrieves the attachment content, setting it in the given 
{@code Attachment} instance
    * </p>
    *
-   * @param attachment
-   *          the {@code Attachment} instance to download and set the content
+   * @param attachment the {@code Attachment} instance to download and set the 
content
    * @throws Exception
    */
   private void retrieveAndSetAttachmentContent(final MutableAttachment 
attachment) throws Exception {
@@ -497,10 +500,19 @@ public class ConfluenceClient {
     logger.debug("[Processing] Hitting url for getting attachment content : 
{}", url);
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
-      attachment.setLength(response.getEntity().getContentLength());
-      final byte[] byteContent = 
IOUtils.toByteArray(response.getEntity().getContent());
-      EntityUtils.consumeQuietly(response.getEntity());
-      attachment.setContentStream(new ByteArrayInputStream(byteContent));
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        if (response.getStatusLine().getStatusCode() != 404) {
+          throw new Exception("Confluence error. " + errorDesc);
+        } else {
+          logger.error("[Processing] Failed to get attachment content {}. 
Error: {}", url, errorDesc);
+        }
+      } else {
+        attachment.setLength(response.getEntity().getContentLength());
+        final byte[] byteContent = 
IOUtils.toByteArray(response.getEntity().getContent());
+        EntityUtils.consumeQuietly(response.getEntity());
+        attachment.setContentStream(new ByteArrayInputStream(byteContent));
+      }
     } catch (final Exception e) {
 
       logger.error("[Processing] Failed to get attachment content from {}. 
Error: {}", url, e.getMessage());
@@ -513,9 +525,8 @@ public class ConfluenceClient {
    * <p>
    * Get a Confluence page identified by its id
    * </p>
-   * 
-   * @param pageId
-   *          the page id
+   *
+   * @param pageId the page id
    * @return the Confluence page
    * @throws Exception
    */
@@ -525,6 +536,15 @@ public class ConfluenceClient {
     logger.debug("[Processing] Hitting url for getting document content : {}", 
url);
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        if (response.getStatusLine().getStatusCode() != 404) {
+          throw new Exception("Confluence error. " + errorDesc);
+        } else {
+          logger.error("[Processing] Failed to get page {}. Error: {}", url, 
errorDesc);
+          return null;
+        }
+      }
       final HttpEntity entity = response.getEntity();
       final MutablePage page = pageFromHttpEntity(entity);
       EntityUtils.consume(entity);
@@ -547,9 +567,8 @@ public class ConfluenceClient {
    * <p>
    * Get the labels of a specific page
    * </p>
-   * 
-   * @param pageId
-   *          The pageId to get the labels
+   *
+   * @param pageId The pageId to get the labels
    * @return a {@code List<Label>} of labels
    */
   public List<Label> getLabels(final String pageId) {
@@ -565,9 +584,13 @@ public class ConfluenceClient {
       try {
         @SuppressWarnings("unchecked")
         final ConfluenceResponse<Label> response = (ConfluenceResponse<Label>) 
getConfluenceResources(url, Label.builder());
-        labels.addAll(response.getResults());
-        lastStart += response.getResults().size();
-        isLast = response.isLast();
+        if (response != null) {
+          labels.addAll(response.getResults());
+          lastStart += response.getResults().size();
+          isLast = response.isLast();
+        } else {
+          break;
+        }
       } catch (final Exception e) {
         logger.debug("Error getting labels for page {}. Reason: {}", pageId, 
e.getMessage());
       }
@@ -589,7 +612,9 @@ public class ConfluenceClient {
       authorities.add("group-" + group.getName());
     });
     final User user = getConfluenceUser(username);
-    authorities.add("user-" + user.getUserKey());
+    if (user != null) {
+      authorities.add("user-" + user.getUserKey());
+    }
     final List<Space> spaces = getSpaces();
     for (final Space space : spaces) {
       final List<String> permissions = getSpacePermissionsForUser(space, 
username);
@@ -605,6 +630,15 @@ public class ConfluenceClient {
     final String url = String.format(Locale.ROOT, 
"%s://%s:%s%s%s?username=%s", protocol, host, port, path, USER_PATH, username);
     final HttpGet httpGet = createGetRequest(url);
     try (CloseableHttpResponse response = executeRequest(httpGet);) {
+      if (response.getStatusLine().getStatusCode() != 200) {
+        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
+        if (response.getStatusLine().getStatusCode() != 404) {
+          throw new Exception("Confluence error. " + errorDesc);
+        } else {
+          logger.error("[Processing] Failed to get page {}. Error: {}", url, 
errorDesc);
+          return null;
+        }
+      }
       final HttpEntity entity = response.getEntity();
       final User user = userFromHttpEntity(entity);
       EntityUtils.consume(entity);
@@ -626,16 +660,20 @@ public class ConfluenceClient {
     do {
       final ConfluenceResponse<Group> response = getUserGroups((int) 
lastStart, (int) defaultSize, username);
 
-      int count = 0;
-      for (final Group group : response.getResults()) {
-        groups.add(group);
-        count++;
-      }
+      if (response != null) {
+        int count = 0;
+        for (final Group group : response.getResults()) {
+          groups.add(group);
+          count++;
+        }
 
-      lastStart += count;
-      isLast = response.isLast();
-      if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) {
-        Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+        lastStart += count;
+        isLast = response.isLast();
+        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
+          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1}", Locale.ROOT).format(new Object[] { lastStart, defaultSize }));
+        }
+      } else {
+        break;
       }
     } while (!isLast);
 
@@ -662,9 +700,8 @@ public class ConfluenceClient {
    * <p>
    * Execute the given {@code HttpUriRequest} using the configured client
    * </p>
-   * 
-   * @param request
-   *          the {@code HttpUriRequest} to be executed
+   *
+   * @param request the {@code HttpUriRequest} to be executed
    * @return the {@code HttpResponse} object returned from the server
    * @throws Exception
    */
@@ -675,11 +712,6 @@ public class ConfluenceClient {
     CloseableHttpResponse response = null;
     try {
       response = httpClient.execute(request, httpContext);
-      if (response.getStatusLine().getStatusCode() != 200) {
-        final String errorDesc = response.getStatusLine().getStatusCode() + " 
" + response.getStatusLine().getReasonPhrase();
-        response.close();
-        throw new Exception("Confluence error. " + errorDesc);
-      }
       return response;
     } catch (final Exception e) {
       if (response != null) {
@@ -694,9 +726,8 @@ public class ConfluenceClient {
    * <p>
    * Creates a Confluence user object from the given entity returned by the 
server
    * </p>
-   * 
-   * @param entity
-   *          the {@code HttpEntity} to create the {@code User}
+   *
+   * @param entity the {@code HttpEntity} to create the {@code User}
    * @return the Confluence user instance
    * @throws Exception
    */
@@ -713,9 +744,8 @@ public class ConfluenceClient {
    * <p>
    * Creates a Confluence page object from the given entity returned by the 
server
    * </p>
-   * 
-   * @param entity
-   *          the {@code HttpEntity} to create the {@code MutablePage} from
+   *
+   * @param entity the {@code HttpEntity} to create the {@code MutablePage} 
from
    * @return the Confluence page instance
    * @throws Exception
    */
@@ -733,9 +763,8 @@ public class ConfluenceClient {
    * <p>
    * Creates a {@code MutableAttachment} object from the given entity returned 
by the server
    * </p>
-   * 
-   * @param entity
-   *          the {@code HttpEntity} to create the {@code MutableAttachment} 
from
+   *
+   * @param entity the {@code HttpEntity} to create the {@code 
MutableAttachment} from
    * @return the Confluence MutableAttachment instance
    * @throws Exception
    */
@@ -752,7 +781,7 @@ public class ConfluenceClient {
    * <p>
    * Method to check if basic authentication must be used
    * </p>
-   * 
+   *
    * @return {@code Boolean} indicating whether basic authentication must be 
used or not
    */
   private boolean useBasicAuthentication() {
@@ -764,8 +793,7 @@ public class ConfluenceClient {
    * Sanitize the given url replacing the appearance of more than one slash by 
only one slash
    * </p>
    *
-   * @param url
-   *          The url to sanitize
+   * @param url The url to sanitize
    * @return the sanitized url
    */
   private String sanitizeUrl(final String url) {
@@ -801,12 +829,16 @@ public class ConfluenceClient {
     do {
       final ConfluenceResponse<Space> response = getSpaces((int) lastStart, 
(int) defaultSize, Optional.<String>absent(), Optional.<String>absent());
 
-      spaces.addAll(response.getResults());
+      if (response != null) {
+        spaces.addAll(response.getResults());
 
-      lastStart += response.getResults().size();
-      isLast = response.isLast();
-      if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) {
-        Logging.connectors.debug(new MessageFormat("New start {0} and size {1} 
for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getSpaces" }));
+        lastStart += response.getResults().size();
+        isLast = response.isLast();
+        if (Logging.connectors != null && Logging.connectors.isDebugEnabled()) 
{
+          Logging.connectors.debug(new MessageFormat("New start {0} and size 
{1} for {2}", Locale.ROOT).format(new Object[] { lastStart, defaultSize, 
"getSpaces" }));
+        }
+      } else {
+        break;
       }
     } while (!isLast);
     return spaces;


Reply via email to