Android Open Source - bv-android-sdk Bazaar Params






From Project

Back to project page bv-android-sdk.

License

The source code is released under:

Apache License

If you think the Android project bv-android-sdk listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright 2013 Bazaarvoice//from www .  j  av  a  2 s.  co  m
 * 
 * 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 com.bazaarvoice;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 * The base class for adding parameters to a BazaarRequest. This
 * class should not be used to add parameters. Instead, use one of the derived
 * classes: {@link DisplayParams}, {@link SubmissionParams}, {@link SubmissionMediaParams}.
 */
public abstract class BazaarParams {

  protected Media media;
  
  /**
   * Get the media file associated with these parameters.
   * 
   * @return the media file or null if there is none
   */
  public Media getMedia() {
    return media;
  }
  
  /**
   * Convert the class into a url parameters string.
   * 
   * @param url
   *            the base url to append to
   * @return the url with parameters
   */
  protected abstract String toURL(String apiVersion, String passKey);
  
  
  /**
   * Add multipart parameters to the Http request
   * 
   * @param name
   *            the name of the parameter being added
   * @param val
   *         The value of the parameter
   */
  protected abstract void addPostParameters(String apiVersion, String passKey, BazaarRequest request);
  
  /**
   * URL encode the value
   * 
   * @param value
   *            the value to encode
   * @return encoded value
   */
  @SuppressWarnings("deprecation")
  protected static String encode(String value) {
    try {
      return URLEncoder.encode(value, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      return URLEncoder.encode(value);
    }
  }
  
  
  /**
   * Helper method for ensuring a map exists and adding a value to it.
   * 
   * @param map
   *            the map
   * @param type
   *            the key
   * @param value
   *            the value
   * @return the original map if map is not null, a new map otherwise
   */
  protected Map<String, String> addToMap(Map<String, String> map,
      String type, String value) {
    if (map == null) {
      map = new HashMap<String, String>();
    }

    String oldValue = map.get(type);
    if (oldValue != null) {
      value = oldValue + ":" + value;
    }
    map.put(type, value);
    return map;
  }
  
  
  protected void addMultipartParameter(String name, Object val, BazaarRequest request) {
    if (name != null && val != null) {
      String value = val.toString();
      
      String topBoundary = "--" + request.boundary + "\r\n";
      String contentDisp = String.format("Content-Disposition: form-data; name=\"%s\"\r\n\r\n", name);
      String valueParam = String.format("%s\r\n", value);
      
      request.multiPartParams.add(topBoundary);
      request.multiPartParams.add(contentDisp);
      request.multiPartParams.add(valueParam);
      
      request.contentLength = request.contentLength + topBoundary.getBytes().length + contentDisp.getBytes().length + valueParam.getBytes().length;
          
      request.multipart = true;    
    }
    }
  
  //adding a file object to request
  protected void addMultipartParameter(String name, String fileName, File mediaFile, BazaarRequest request) {
      
        if ((name != null) && (fileName != null)) {
          
          String topBoundary = "--" + request.boundary + "\r\n";
      String contentDisp = String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", 
            name, fileName);
      String valueParam = "\r\n";
      
      request.mediaParam.add(topBoundary);
      request.mediaParam.add(contentDisp);
      request.mediaParam.add(valueParam);      
      
      request.contentLength = request.contentLength + topBoundary.getBytes().length + contentDisp.getBytes().length + valueParam.getBytes().length + (int) mediaFile.length();
          
      request.multipart = true;
      request.media = true;
        }
    }
  
  //adding byte array to the request
  protected void addMultipartParameter(String name, String fileName, byte[] mediaFileBytes, BazaarRequest request) {
      
        if ((name != null) && (fileName != null)) {
          
          String topBoundary = "--" + request.boundary + "\r\n";
      String contentDisp = String.format("Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\nContent-Type: application/octet-stream\r\n\r\n", 
            name, fileName);
      String valueParam = "\r\n";
      
      request.mediaParam.add(topBoundary);
      request.mediaParam.add(contentDisp);
      request.mediaParam.add(valueParam);      
      
      request.contentLength = request.contentLength + topBoundary.getBytes().length + contentDisp.getBytes().length + valueParam.getBytes().length + mediaFileBytes.length;
          
      request.multipart = true;
      request.media = true;
        }
    }
  
  
  protected String addURLParameter(String name, Object val) {
    if (val != null) {
      String value = val.toString();
      
      if (value.length() > 0) {
        value = BazaarParams.encode(value);
        return "&" + name + '=' + value;
      }
    }
    return "";
  }  
  
  
  protected String addURLParameter(String name, Map<String, String> map) {
    StringBuilder url = new StringBuilder();
    if (map != null) {
      for (Map.Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        url.append(addURLParameter(name + "_" + key, value));
      }
    }
    return url.toString();
  }
  
  
  protected String addURLParameter(String name, List<String> values) {
    if (values != null) {
      StringBuilder paramList = new StringBuilder();
      boolean first = true;
      for (String value : values) {
        if (first) {
          first = false;
        } else {
          paramList.append(",");
        }

        paramList.append(value);
      }
      return addURLParameter(name, paramList);
    }
    return "";
  }

  
  protected String addNthURLParamsFromList(String name, List<String> values) {
    StringBuilder url = new StringBuilder();
    if (values != null) {
      for (int i = 0; i < values.size(); i++) {
        // Start with param_1
        int key = i + 1;
        String value = values.get(i);

        url.append(addURLParameter(name + "_" + key, value));
      }
    }
    return url.toString();
  }

}




Java Source Code List

com.bazaarvoice.BazaarException.java
com.bazaarvoice.BazaarParams.java
com.bazaarvoice.BazaarRequest.java
com.bazaarvoice.DisplayParams.java
com.bazaarvoice.Media.java
com.bazaarvoice.OnBazaarResponse.java
com.bazaarvoice.SubmissionMediaParams.java
com.bazaarvoice.SubmissionParams.java
com.bazaarvoice.example.browseproducts.BazaarFunctions.java
com.bazaarvoice.example.browseproducts.BazaarProduct.java
com.bazaarvoice.example.browseproducts.BazaarReview.java
com.bazaarvoice.example.browseproducts.BazaarUIThreadResponse.java
com.bazaarvoice.example.browseproducts.ImageDownloader.java
com.bazaarvoice.example.browseproducts.MainActivity.java
com.bazaarvoice.example.browseproducts.OnImageDownloadComplete.java
com.bazaarvoice.example.browseproducts.ProductAdapter.java
com.bazaarvoice.example.browseproducts.ProductsActivity.java
com.bazaarvoice.example.browseproducts.ReviewAdapter.java
com.bazaarvoice.example.browseproducts.ReviewDisplayActivity.java
com.bazaarvoice.example.browseproducts.ReviewsActivity.java
com.bazaarvoice.example.reviewsubmission.BazaarFunctions.java
com.bazaarvoice.example.reviewsubmission.BazaarReview.java
com.bazaarvoice.example.reviewsubmission.CameraUtils.java
com.bazaarvoice.example.reviewsubmission.ImageDownloader.java
com.bazaarvoice.example.reviewsubmission.MainActivity.java
com.bazaarvoice.example.reviewsubmission.OnImageDownloadComplete.java
com.bazaarvoice.example.reviewsubmission.OnImageUploadComplete.java
com.bazaarvoice.example.reviewsubmission.RatingActivity.java
com.bazaarvoice.example.reviewsubmission.RatingPreviewActivity.java
com.bazaarvoice.example.reviewsubmission.ReviewSubmissionApp.java
com.bazaarvoice.intentexample.BazaarFunctions.java
com.bazaarvoice.intentexample.BazaarUIThreadResponse.java
com.bazaarvoice.intentexample.CameraUtils.java
com.bazaarvoice.intentexample.MainActivity.java
com.bazaarvoice.types.Action.java
com.bazaarvoice.types.ApiVersion.java
com.bazaarvoice.types.Equality.java
com.bazaarvoice.types.FeedbackContentType.java
com.bazaarvoice.types.FeedbackType.java
com.bazaarvoice.types.FeedbackVoteType.java
com.bazaarvoice.types.IncludeStatsType.java
com.bazaarvoice.types.IncludeType.java
com.bazaarvoice.types.MediaParamsContentType.java
com.bazaarvoice.types.RequestType.java
com.chute.android.photopickerplus.adapter.AlbumsAdapter.java
com.chute.android.photopickerplus.adapter.PhotoSelectCursorAdapter.java
com.chute.android.photopickerplus.adapter.PhotosAdapter.java
com.chute.android.photopickerplus.app.AlbumsActivity.java
com.chute.android.photopickerplus.app.ChooseServiceActivity.java
com.chute.android.photopickerplus.app.GridActivity.java
com.chute.android.photopickerplus.app.PhotoPickerPlusApp.java
com.chute.android.photopickerplus.dao.MediaDAO.java
com.chute.android.photopickerplus.util.AppUtil.java
com.chute.android.photopickerplus.util.Constants.java
com.chute.android.photopickerplus.util.NotificationUtil.java
com.chute.android.photopickerplus.util.intent.AlbumsActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.IntentUtil.java
com.chute.android.photopickerplus.util.intent.IntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoActivityIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotoPickerPlusIntentWrapper.java
com.chute.android.photopickerplus.util.intent.PhotosIntentWrapper.java
com.example.productwidgetexample.BazaarFunctions.java
com.example.productwidgetexample.BazaarProduct.java
com.example.productwidgetexample.BazaarReview.java
com.example.productwidgetexample.BazaarUIThreadResponse.java
com.example.productwidgetexample.ImageDownloader.java
com.example.productwidgetexample.OnImageDownloadComplete.java
com.example.productwidgetexample.ProductWidgetProvider.java
com.example.productwidgetexample.ReviewAdapter.java
com.example.productwidgetexample.ReviewsActivity.java