// Path: dashboard/src/ch/icclab/cyclops/dashboard/oauth2/OAuthClientResource.java
// public class OAuthClientResource extends ClientResource {
//     private String oauthToken;
// 
//     public OAuthClientResource(String url, String oauthToken) {
//         super(url);
//         this.oauthToken = oauthToken;
//     }
// 
//     private void addTokenHeader() {
//         ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
//         challenge.setRawValue(oauthToken);
//         setChallengeResponse(challenge);
//     }
// 
//     /**
//      * This method adds the OAUTH header to the client resource before sending it.
//      * Sending the request will be handled by the parent class.
//      */
//     @Override
//     public Representation get() {
//         addTokenHeader();
//         return super.get();
//     }
// 
//     /**
//      * This method adds the OAUTH header to the client resource before sending it.
//      * Sending the request will be handled by the parent class.
//      */
//     @Override
//     public Representation post(Representation rep) {
//         addTokenHeader();
//         return super.post(rep);
//     }
// }
// 
// Path: dashboard/src/ch/icclab/cyclops/dashboard/oauth2/OAuthServerResource.java
// public class OAuthServerResource extends ServerResource {
// 
//     /**
//      * This method returns the OAUTH header if availabe
//      * @return OAUTH token or empty string
//      */
//     public String getOAuthTokenFromHeader() {
//         Series<Header> headers = (Series<Header>) getRequestAttributes().get("org.restlet.http.headers");
// 
//         if(headers == null) {
//             return "";
//         }
// 
//         return headers.getFirstValue("X-OAuth-Token", true, "");
//     }
// }
// 
// Path: dashboard/src/ch/icclab/cyclops/dashboard/util/LoadConfiguration.java
// public class LoadConfiguration extends ClientResource {
// 
//     //Instantiating the Instance variable for saving the config details
//     public static HashMap<String,String> configuration;
// 
//     /**
//      * Loads the configuration file
//      *
//      * Pseudo Code
//      * 1. Create an instance of the ServletContext
//      * 2. Get the relative path of the configuration.txt file
//      * 3. Load the file and save the values into a static HashMap
//      *
//      * @param context
//      * @throws IOException
//      */
//     public void run(Context context) throws IOException {
//         configuration = new HashMap<String,String>();
//         String nextLine;
// 
//         // Extract the ServletContext from the attributes of RestletContext
//         ServletContext servlet = (ServletContext) context.getAttributes().get("org.restlet.ext.servlet.ServletContext");
//         System.out.println("Reading the config file from " + servlet.getRealPath("/WEB-INF/configuration.txt"));
//         // Get the path of the config file relative to the WAR
//         String rootPath = servlet.getRealPath("/WEB-INF/configuration.txt");
// 
//         //Add WEB-INF path to access local images etc.
//         String webInfPath = servlet.getRealPath("/WEB-INF");
//         configuration.put("WEB-INF", webInfPath);
// 
//         Path path = Paths.get(rootPath);
//         File configFile = new File(path.toString());
//         FileRepresentation file = new FileRepresentation(configFile, MediaType.TEXT_PLAIN);
// 
//         // Read the values from the config file
//         try {
//             BufferedReader reader = new BufferedReader(file.getReader());
//             while((nextLine = reader.readLine()) != null ) {
//                 String[] str = nextLine.split("==");
//                 configuration.put(str[0],str[1]);
//             }
//         } catch (IOException e) {
//             System.out.println("Failed to load the Config file");
//             e.printStackTrace();
//         }
//     }
// }


import ch.icclab.cyclops.dashboard.oauth2.OAuthClientResource;
import ch.icclab.cyclops.dashboard.oauth2.OAuthServerResource;
import ch.icclab.cyclops.dashboard.util.LoadConfiguration;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
/*
 * Copyright (c) 2015. Zuercher Hochschule fuer Angewandte Wissenschaften
 *  All Rights Reserved.
 *
 *     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 ch.icclab.cyclops.dashboard.charge;


/**
 * This class handles all requests concerning the Charge microservice
 */
public class Charge extends OAuthServerResource {
    /**
     * This method gets the charge data from the RC microservice.
     *
     * The method receives the the user's keystone ID and two timestamps (from / to) from the dashboard frontend.
     * It then sends this information to the RC Endpoint, requesting the usage data during the given time frame. The
     * RC Charge endpoint is configured in /WEB-INF/configuration.txt as RC_CHARGE_URL
     *
     * @return [description]
     */
    @Get
    public Representation getCharge() {
        String query = getRequest().getResourceRef().getQuery();
        String oauthToken = getOAuthTokenFromHeader();
// Path: dashboard/src/ch/icclab/cyclops/dashboard/oauth2/OAuthClientResource.java
// public class OAuthClientResource extends ClientResource {
//     private String oauthToken;
// 
//     public OAuthClientResource(String url, String oauthToken) {
//         super(url);
//         this.oauthToken = oauthToken;
//     }
// 
//     private void addTokenHeader() {
//         ChallengeResponse challenge = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
//         challenge.setRawValue(oauthToken);
//         setChallengeResponse(challenge);
//     }
// 
//     /**
//      * This method adds the OAUTH header to the client resource before sending it.
//      * Sending the request will be handled by the parent class.
//      */
//     @Override
//     public Representation get() {
//         addTokenHeader();
//         return super.get();
//     }
// 
//     /**
//      * This method adds the OAUTH header to the client resource before sending it.
//      * Sending the request will be handled by the parent class.
//      */
//     @Override
//     public Representation post(Representation rep) {
//         addTokenHeader();
//         return super.post(rep);
//     }
// }
// 
// Path: dashboard/src/ch/icclab/cyclops/dashboard/oauth2/OAuthServerResource.java
// public class OAuthServerResource extends ServerResource {
// 
//     /**
//      * This method returns the OAUTH header if availabe
//      * @return OAUTH token or empty string
//      */
//     public String getOAuthTokenFromHeader() {
//         Series<Header> headers = (Series<Header>) getRequestAttributes().get("org.restlet.http.headers");
// 
//         if(headers == null) {
//             return "";
//         }
// 
//         return headers.getFirstValue("X-OAuth-Token", true, "");
//     }
// }
// 
// Path: dashboard/src/ch/icclab/cyclops/dashboard/util/LoadConfiguration.java
// public class LoadConfiguration extends ClientResource {
// 
//     //Instantiating the Instance variable for saving the config details
//     public static HashMap<String,String> configuration;
// 
//     /**
//      * Loads the configuration file
//      *
//      * Pseudo Code
//      * 1. Create an instance of the ServletContext
//      * 2. Get the relative path of the configuration.txt file
//      * 3. Load the file and save the values into a static HashMap
//      *
//      * @param context
//      * @throws IOException
//      */
//     public void run(Context context) throws IOException {
//         configuration = new HashMap<String,String>();
//         String nextLine;
// 
//         // Extract the ServletContext from the attributes of RestletContext
//         ServletContext servlet = (ServletContext) context.getAttributes().get("org.restlet.ext.servlet.ServletContext");
//         System.out.println("Reading the config file from " + servlet.getRealPath("/WEB-INF/configuration.txt"));
//         // Get the path of the config file relative to the WAR
//         String rootPath = servlet.getRealPath("/WEB-INF/configuration.txt");
// 
//         //Add WEB-INF path to access local images etc.
//         String webInfPath = servlet.getRealPath("/WEB-INF");
//         configuration.put("WEB-INF", webInfPath);
// 
//         Path path = Paths.get(rootPath);
//         File configFile = new File(path.toString());
//         FileRepresentation file = new FileRepresentation(configFile, MediaType.TEXT_PLAIN);
// 
//         // Read the values from the config file
//         try {
//             BufferedReader reader = new BufferedReader(file.getReader());
//             while((nextLine = reader.readLine()) != null ) {
//                 String[] str = nextLine.split("==");
//                 configuration.put(str[0],str[1]);
//             }
//         } catch (IOException e) {
//             System.out.println("Failed to load the Config file");
//             e.printStackTrace();
//         }
//     }
// }


// Path: dashboard/src/ch/icclab/cyclops/dashboard/charge/Charge.java
import ch.icclab.cyclops.dashboard.oauth2.OAuthClientResource;
import ch.icclab.cyclops.dashboard.oauth2.OAuthServerResource;
import ch.icclab.cyclops.dashboard.util.LoadConfiguration;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;

/*
 * Copyright (c) 2015. Zuercher Hochschule fuer Angewandte Wissenschaften
 *  All Rights Reserved.
 *
 *     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 ch.icclab.cyclops.dashboard.charge;


/**
 * This class handles all requests concerning the Charge microservice
 */
public class Charge extends OAuthServerResource {
    /**
     * This method gets the charge data from the RC microservice.
     *
     * The method receives the the user's keystone ID and two timestamps (from / to) from the dashboard frontend.
     * It then sends this information to the RC Endpoint, requesting the usage data during the given time frame. The
     * RC Charge endpoint is configured in /WEB-INF/configuration.txt as RC_CHARGE_URL
     *
     * @return [description]
     */
    @Get
    public Representation getCharge() {
        String query = getRequest().getResourceRef().getQuery();
        String oauthToken = getOAuthTokenFromHeader();
        String url = LoadConfiguration.configuration.get("RC_CHARGE_URL") + "?" + query;