<commit_before>/*
 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (http://www.leroymerlin.fr/) - initial implementation
 */

package org.nuxeo.opensocial.container.factory.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.spaces.api.Gadget;
import org.nuxeo.opensocial.container.client.bean.PreferencesBean;
import org.nuxeo.opensocial.container.factory.PreferenceManager;
import org.nuxeo.opensocial.container.utils.SecureTokenBuilder;
import org.nuxeo.opensocial.gadgets.service.api.GadgetService;
import org.nuxeo.runtime.api.Framework;

/**
 * URlBuilder is builder of gadget url; Util for render of gadget into Shinding
 * opensocial server
 * 
 */
public class UrlBuilder {

  private static final String SERVLET_PATH = "/nuxeo/opensocial/gadgets/ifr";

  private static final String CONTAINER_KEY = "container";
  private static final String CONTAINER_VALUE = "default";

  private static final String GADGET_ID_KEY = "mid";

  private static final String NOCACHE_KEY = "nocache";
  private static final String NOCACHE_VALUE = "1";

  private static final String COUNTRY_KEY = "country";
  private static final String COUNTRY_VALUE = "ALL";

  private static final String LANG_KEY = "lang";
  private static final String LANG_VALUE = "ALL";

  private static final String VIEW_KEY = "view";
  private static final String VIEW_VALUE = "default";

  private static final String PERMISSION_KEY = "permission";

  private static final String URL_KEY = "url";

  private static final String PREF_PREFIX = "up_";

  private static final String SECURITY_TOKEN_KEY = "st";

  private static final String RPC_TOKEN = "<PASSWORD>";

  private static final Log log = LogFactory.getLog(UrlBuilder.class);

  private static final String PARENT_KEY = "parent";

  private static int containerId = 0;

  public static String buildShindigUrl(Gadget gadget, boolean permission)
      throws ClientException {

    // TODO: get away prefs definition from the url
    // TODO: Better implementation of RPC_Token that relies now on gadget id

    String gadgetDef = gadget.getDefinitionUrl()
        .toString();

    StringBuilder sb = new StringBuilder();

    // http://localhost:8080/nuxeo/opensocial/gadgets/ifr?
    sb.append(ServerBase.getBase() + SERVLET_PATH + "?");

    // container=default&nocache=1&country=ALL&lang=ALL&view=default&
    sb.append(getDefaultParams() + "&");

    // mid=123456&
    sb.append(GADGET_ID_KEY + "=" + containerId++ + "&");

    // parent=http://localhost:8080/...
    sb.append(PARENT_KEY + "=" + ServerBase.getBase() + "&");

    // perm=1 -> does the session has write perm on gadget
    sb.append(PERMISSION_KEY + "=" + permission + "&");

    // url=http://.../gadget.xml?up_prefname=prefvalue
    sb.append(URL_KEY + "=" + gadgetDef
        + getUserPrefs(PreferenceManager.getPreferences(gadget)) + "&");

    // up_=150&
    sb.append("up_" + gadget.getHeight() + "&");

    // st=qdlfjqsmfkjqf&
    try {
      sb.append(SECURITY_TOKEN_KEY + "=" + getSecurityToken(gadget, gadgetDef));
    } catch (Exception e) {
      throw new ClientException("Unable to getSecurity Token", e);
    }

    // #rpctoken=<PASSWORD>5
    sb.append("#" + RPC_TOKEN + "=" + gadget.getId());

    return sb.toString();

  }

  /**
   * Get Gadget Definition with GadgetService Gadget Definition is google xml
   * gadget
   * 
   * @param name
   * @return
   * @throws Exception
   */
  public static String getGadgetDef(String name) throws Exception {
    return Framework.getService(GadgetService.class)
        .getGadgetDefinition(name)
        .toString();
  }

  private static String getSecurityToken(Gadget gadget, String url)
      throws Exception {
    return SecureTokenBuilder.getSecureToken(gadget.getViewer(),
        gadget.getOwner(), url);
  }

  /**
   * Build a url format parameters with preferences of gadget Util for render
   * gadget into Shinding opensocial server
   * 
   * @param prefs
   * @return String &up_key=value&up..
   */<commit_msg>NXP-4585 Fix some auth token problems<commit_after>/*
 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     <NAME> (http://www.leroymerlin.fr/) - initial implementation
 */

package org.nuxeo.opensocial.container.factory.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.spaces.api.Gadget;
import org.nuxeo.opensocial.container.client.bean.PreferencesBean;
import org.nuxeo.opensocial.container.factory.PreferenceManager;
import org.nuxeo.opensocial.container.utils.SecureTokenBuilder;
import org.nuxeo.opensocial.gadgets.service.api.GadgetService;
import org.nuxeo.runtime.api.Framework;

/**
 * URlBuilder is builder of gadget url; Util for render of gadget into Shinding
 * opensocial server
 *
 */
public class UrlBuilder {

  private static final String SERVLET_PATH = "/nuxeo/opensocial/gadgets/ifr";

  private static final String CONTAINER_KEY = "container";
  private static final String CONTAINER_VALUE = "default";

  private static final String GADGET_ID_KEY = "mid";

  private static final String NOCACHE_KEY = "nocache";
  private static final String NOCACHE_VALUE = "1";

  private static final String COUNTRY_KEY = "country";
  private static final String COUNTRY_VALUE = "ALL";

  private static final String LANG_KEY = "lang";
  private static final String LANG_VALUE = "ALL";

  private static final String VIEW_KEY = "view";
  private static final String VIEW_VALUE = "default";

  private static final String PERMISSION_KEY = "permission";

  private static final String URL_KEY = "url";

  private static final String PREF_PREFIX = "up_";

  private static final String SECURITY_TOKEN_KEY = "st";

  private static final String RPC_TOKEN = "rpctoken";

  private static final Log log = LogFactory.getLog(UrlBuilder.class);

  private static final String PARENT_KEY = "parent";

  private static int containerId = 0;

  public static String buildShindigUrl(Gadget gadget, boolean permission)
      throws ClientException {

    // TODO: get away prefs definition from the url
    // TODO: Better implementation of RPC_Token that relies now on gadget id

    String gadgetDef = gadget.getDefinitionUrl()
        .toString();

    StringBuilder sb = new StringBuilder();

    // http://localhost:8080/nuxeo/opensocial/gadgets/ifr?
    sb.append(ServerBase.getBase() + SERVLET_PATH + "?");

    // container=default&nocache=1&country=ALL&lang=ALL&view=default&
    sb.append(getDefaultParams() + "&");

    // mid=123456&
    sb.append(GADGET_ID_KEY + "=" + containerId++ + "&");

    // parent=http://localhost:8080/...
    sb.append(PARENT_KEY + "=" + ServerBase.getBase() + "&");

    // perm=1 -> does the session has write perm on gadget
    sb.append(PERMISSION_KEY + "=" + permission + "&");

    // url=http://.../gadget.xml?up_prefname=prefvalue
    sb.append(URL_KEY + "=" + gadgetDef
        + getUserPrefs(PreferenceManager.getPreferences(gadget)) + "&");

    // up_=150&
    sb.append("up_" + gadget.getHeight() + "&");

    // st=qdlfjqsmfkjqf&
    try {
      sb.append(SECURITY_TOKEN_KEY + "=" + getSecurityToken(gadget, gadgetDef));
    } catch (Exception e) {
      throw new ClientException("Unable to getSecurity Token", e);
    }

    // #rpctoken=<PASSWORD>
    sb.append("&" + RPC_TOKEN + "=" + gadget.getId());

    return sb.toString();

  }

  /**
   * Get Gadget Definition with GadgetService Gadget Definition is google xml
   * gadget
   *
   * @param name
   * @return
   * @throws Exception
   */
  public static String getGadgetDef(String name) throws Exception {
    return Framework.getService(GadgetService.class)
        .getGadgetDefinition(name)
        .toString();
  }

  private static String getSecurityToken(Gadget gadget, String url)
      throws Exception {
    return SecureTokenBuilder.getSecureToken(gadget.getViewer(),
        gadget.getOwner(), url);
  }

  /**
   * Build a url format parameters with preferences of gadget Util for render
   * gadget into Shinding opensocial server
   *
   * @param prefs
   * @return String &up_key=value&up..
   */
{'fraction_non_alphanumeric': 0.10924457429048413, 'fraction_numerical': 0.006990818030050083, 'mean_word_length': 3.7639165009940356, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3443173', 'n_tokens_mistral': 3005, 'n_tokens_neox': 2888, 'n_words': 801}