package fi.laverca.registration;

import java.io.IOException;
import java.util.List;

import fi.laverca.jaxb.mreg.RegistrationInput;
import fi.laverca.jaxb.mss.MSSRegistrationReq;
import fi.laverca.jaxb.mss.MSSRegistrationResp;
import fi.laverca.mss.MssClient;
import fi.laverca.mss.MssConf;
import fi.laverca.util.LavercaContext;

/**
 * Client specifically for handling MSS_Registration operations
 * 
 */
public class RegistrationClient {

    private final MssClient client;
   
    /**
     * Default constructor with {@link MssConf}
     * <p>The configuration should contain at least the following options:
     * 
     * <ul>
     * <li>{@link MssConf#REGISTRATION_URL} - where to send the MSS_RegistrationRequests
     * <li>{@link MssConf#AP_ID}            - what AP_ID to use in the requests
     * <li>{@link MssConf#KEYSTORE_FILE}    - keystore to use
     * <li>{@link MssConf#KEYSTORE_PWD}     - keystore password
     * </ul>
     * @param conf Configuration for this client's MSsClient. 
     */
    public RegistrationClient(final MssConf conf) {
        this.client = new MssClient(conf);
    }
    
    /**
     * Send an MSS_RegistrationRequest (with MReg extension)
     * 
     * @param req MReg Request
     * @return parsed MReg response
     * @throws IOException
     */
    public MregResponse send(final MregRequest req) throws IOException {
        req.context = new LavercaContext();
        MSSRegistrationReq  _req  = req.toMSSReq(this.client);
        MSSRegistrationResp _resp = this.client.send(_req, req.context);
        return new MregResponse(_resp, req.context);
    }
    
    /**
     * Send a multi-operation MSS_RegistrationRequest (with MReg extension)
     * <p>Reads the target and AP_TransID from {@code req}. Adds a new RegistrationInput for each element in {@code additionalRequests}
     * @param req MReg Request
     * @param additionalRequests Additional requests (only operation content is fetched from these - target is ignored
     * @return parsed MReg response
     * @throws IOException
     */
    public MregResponse send(final MregRequest req, 
                             final List<MregRequest> additionalRequests) throws IOException {
        
        req.context = new LavercaContext();
        MSSRegistrationReq _final = req.toMSSReq(this.client);
        
        int i = 100;
        for (MregRequest data : additionalRequests) {
            MSSRegistrationReq _data = data.toMSSReq(this.client);
            RegistrationInput _input = _data.getRegistrationInputs().get(0);
            _input.setInputId("_" + i++);
            
            _final.getRegistrationInputs().add(_input);
        }
        MSSRegistrationResp _resp = this.client.send(_final, req.context);
        
        return new MregResponse(_resp, req.context);
    }
    
    /**
     * Get the raw {@link MssClient}
     * @return {@link MssClient} instance
     */
    public MssClient getMssClient() {
        return this.client;
    }
    
}

@@ -1,8 +1,784 @@
+/* ==========================================%0D%0A * Laverca Project%0D%0A * https://sourceforge.net/projects/laverca/%0D%0A * ==========================================%0D%0A * Copyright 2015 Laverca Project%0D%0A * %0D%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0D%0A * you may not use this file except in compliance with the License.%0D%0A * You may obtain a copy of the License at%0D%0A * %0D%0A *      http://www.apache.org/licenses/LICENSE-2.0%0D%0A * %0D%0A * Unless required by applicable law or agreed to in writing, software%0D%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0D%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0D%0A * See the License for the specific language governing permissions and%0D%0A * limitations under the License.%0D%0A */%0D%0A%0D%0A
 package 
@@ -834,16 +834,42 @@
 ption;%0D%0A
+import java.util.Arrays;%0D%0A
 import j
@@ -2683,16 +2683,700 @@
 uests%7D%0D%0A
+     *%0D%0A     * @param req MReg Request%0D%0A     * @param additionalRequests Additional requests (only operation content is fetched from these - target is ignored%0D%0A     * @return parsed MReg response%0D%0A     * @throws IOException%0D%0A     */%0D%0A    public MregResponse send(final MregRequest req,%0D%0A                             final MregRequest ... additionalRequests) throws IOException %7B%0D%0A         return this.send(req, Arrays.asList(additionalRequests));%0D%0A    %7D%0D%0A    %0D%0A    /**%0D%0A     * Send a multi-operation MSS_RegistrationRequest (with MReg extension)%0D%0A     * %3Cp%3EReads the target and AP_TransID from %7B@code req%7D. Adds a new RegistrationInput for each element in %7B@code additionalRequests%7D%0D%0A
      * @

Improve registration api (added new send() method)