/*
 * Copyright (C) 2017 Reece H. Dunn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.reecedunn.intellij.plugin.marklogic.api.xcc;

import com.marklogic.xcc.Session;
import com.marklogic.xcc.exceptions.RequestException;
import com.marklogic.xcc.exceptions.ServerResponseException;
import org.jetbrains.annotations.NotNull;
import uk.co.reecedunn.intellij.plugin.marklogic.api.Request;
import uk.co.reecedunn.intellij.plugin.marklogic.api.Response;
import uk.co.reecedunn.intellij.plugin.marklogic.api.ResponseException;

import java.io.IOException;

public class XCCRequest implements Request {
    private final Session session;
    private final com.marklogic.xcc.Request request;

    XCCRequest(Session session, com.marklogic.xcc.Request request) {
        this.session = session;
        this.request = request;
    }

    @NotNull
    @Override
    public Response run() throws IOException {
        try {
            return new XCCResponse(session.submitRequest(request), session);
        } catch (ServerResponseException e) {
            throw new ResponseException(e.getResponseCode(), e.getResponseMessage(), null);
        } catch (RequestException | IllegalStateException e) {
            // IllegalStateException is thrown for invalid credentials.
            throw new IOException(e.getMessage(), e);
        }
    }
}

@@ -734,24 +734,88 @@
 tException;%0A
+import com.marklogic.xcc.exceptions.RequestPermissionException;%0A
 import com.m
@@ -1737,118 +1737,163 @@
 uest
-Exception %7C IllegalStateException e) %7B%0A            // IllegalStateException is thrown for invalid credentials.
+PermissionException e) %7B%0A            throw new ResponseException(401, %22Unauthorized%22, null);%0A        %7D catch (RequestException %7C IllegalStateException e) %7B
 %0A   

Throw a 401 Unauthorized exception for XCC RequestPermissionExceptions, to be consistent with the REST API.