/*
 * 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);
        }
    }
}

/*
 * 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.RequestPermissionException;
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 (RequestPermissionException e) {
            throw new ResponseException(401, "Unauthorized", null);
        } catch (RequestException | IllegalStateException e) {
            throw new IOException(e.getMessage(), e);
        }
    }
}

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