Android Open Source - EBrowser Weave Basic Object






From Project

Back to project page EBrowser.

License

The source code is released under:

GNU General Public License

If you think the Android project EBrowser 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 2010 Patrick Woodworth/*from   w  w w .j av a 2s. c  om*/
 *
 * 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 org.emergent.android.weave.client;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Date;

/**
 * @author Patrick Woodworth
 */
public class WeaveBasicObject {

  private URI m_uri = null;
  private final URI m_queryUri;
  private final JSONObject m_nodeObj;

  public WeaveBasicObject(URI queryUri, JSONObject nodeObj) {
    m_queryUri = queryUri;
    m_nodeObj = nodeObj;
  }

  public String getId() throws JSONException {
    return m_nodeObj.getString("id");
  }

  public String getSortIndex() throws JSONException {
    return m_nodeObj.getString("sortindex");
  }

  public String getModified() throws JSONException {
    return m_nodeObj.getString("modified");
  }

  public Date getModifiedDate() throws JSONException {
    return WeaveUtil.toModifiedTimeDate(getModified());
  }
  
  public URI getUri() throws JSONException {
    if (m_uri == null) {
      try {
        String baseUriStr = m_queryUri.toASCIIString();
        String queryPart = m_queryUri.getRawQuery();
        if (queryPart != null)
          baseUriStr = baseUriStr.substring(0, baseUriStr.indexOf(queryPart) - 1);
        if (!baseUriStr.endsWith("/"))
          baseUriStr += "/";
        String nodeUriStr = baseUriStr + new URI(null,null,getId(), null).toASCIIString();
        m_uri = new URI(nodeUriStr);
      } catch (URISyntaxException e) {
        throw new JSONException(e.getMessage());
      }
    }
    return m_uri;
  }

  public JSONObject getPayload() throws JSONException {
    return new JSONObject(m_nodeObj.getString("payload"));
  }

  public JSONObject getEncryptedPayload(UserWeave weave, char[] secret)
      throws JSONException, IOException, GeneralSecurityException, WeaveException {
    WeaveEncryptedObject weo = new WeaveEncryptedObject(getPayload());
    byte[] syncKey = Base32.decodeModified(new String(secret)); // todo don't convert to string
    BulkKeyCouplet bulkKeyPair = weave.getBulkKeyPair(syncKey);
    return weo.decryptObject(bulkKeyPair);
  }

  public JSONObject getEncryptedPayload(Key bulkKey, Key hmacKey)
      throws JSONException, IOException, GeneralSecurityException, WeaveException {
    WeaveEncryptedObject weo = new WeaveEncryptedObject(getPayload());
    return weo.decryptObject(bulkKey, hmacKey);
  }

  public JSONObject toJSONObject() {
    return m_nodeObj;
  }

  public String toJSONObjectString() throws JSONException {
    return toJSONObject().toString(0);
  }

  public static class WeaveEncryptedObject {

    private final JSONObject m_nodeObj;

    public WeaveEncryptedObject(JSONObject nodeObj) {
      m_nodeObj = nodeObj;
    }

    public String getHmac() throws JSONException {
      return m_nodeObj.getString("hmac");
    }

    public String getCiphertext() throws JSONException {
      return m_nodeObj.getString("ciphertext");
    }

    public String getIv() throws JSONException {
      return m_nodeObj.getString("IV");
    }

    public JSONObject decryptObject(BulkKeyCouplet keyPair) throws GeneralSecurityException, JSONException {
      return decryptObject(keyPair.cipherKey, keyPair.hmacKey);
    }

    public JSONObject decryptObject(Key key, Key hmacKey) throws GeneralSecurityException, JSONException {
      byte[] bytes = WeaveCryptoUtil.getInstance().decrypt(key, hmacKey, getCiphertext(), getIv(), getHmac());
      return new JSONObject(WeaveUtil.toUtf8String(bytes));
    }
  }
}




Java Source Code List

org.emergent.android.weave.client.Base32.java
org.emergent.android.weave.client.Base64Encoder.java
org.emergent.android.weave.client.Base64.java
org.emergent.android.weave.client.BulkKeyCouplet.java
org.emergent.android.weave.client.Dbg.java
org.emergent.android.weave.client.HexEncoder.java
org.emergent.android.weave.client.Hex.java
org.emergent.android.weave.client.QueryParams.java
org.emergent.android.weave.client.QueryResult.java
org.emergent.android.weave.client.UserWeave.java
org.emergent.android.weave.client.WeaveAccountInfo.java
org.emergent.android.weave.client.WeaveBasicObject.java
org.emergent.android.weave.client.WeaveConstants.java
org.emergent.android.weave.client.WeaveCryptoUtil.java
org.emergent.android.weave.client.WeaveException.java
org.emergent.android.weave.client.WeaveFactory.java
org.emergent.android.weave.client.WeaveHeader.java
org.emergent.android.weave.client.WeaveResponse.java
org.emergent.android.weave.client.WeaveSSLSocketFactory.java
org.emergent.android.weave.client.WeaveTransport.java
org.emergent.android.weave.client.WeaveUtil.java
org.greendroid.QuickActionGrid.java
org.greendroid.QuickActionWidget.java
org.greendroid.QuickAction.java
org.zirco.controllers.Controller.java
org.zirco.events.EventConstants.java
org.zirco.events.EventController.java
org.zirco.events.IDownloadEventsListener.java
org.zirco.model.DbAdapter.java
org.zirco.model.UrlSuggestionItemComparator.java
org.zirco.model.adapters.BookmarksCursorAdapter.java
org.zirco.model.adapters.DownloadListAdapter.java
org.zirco.model.adapters.HistoryExpandableListAdapter.java
org.zirco.model.adapters.UrlSuggestionCursorAdapter.java
org.zirco.model.adapters.WeaveBookmarksCursorAdapter.java
org.zirco.model.items.BookmarkItem.java
org.zirco.model.items.DownloadItem.java
org.zirco.model.items.HistoryItem.java
org.zirco.model.items.UrlSuggestionItem.java
org.zirco.model.items.WeaveBookmarkItem.java
org.zirco.providers.BookmarksProviderWrapper.java
org.zirco.providers.WeaveColumns.java
org.zirco.providers.WeaveContentProvider.java
org.zirco.providers.ZircoBookmarksContentProvider.java
org.zirco.sync.ISyncListener.java
org.zirco.sync.WeaveSyncTask.java
org.zirco.ui.activities.AboutActivity.java
org.zirco.ui.activities.AdBlockerWhiteListActivity.java
org.zirco.ui.activities.BookmarksHistoryActivity.java
org.zirco.ui.activities.BookmarksListActivity.java
org.zirco.ui.activities.ChangelogActivity.java
org.zirco.ui.activities.DownloadsListActivity.java
org.zirco.ui.activities.EditBookmarkActivity.java
org.zirco.ui.activities.HistoryListActivity.java
org.zirco.ui.activities.IToolbarsContainer.java
org.zirco.ui.activities.MainActivity.java
org.zirco.ui.activities.MobileViewListActivity.java
org.zirco.ui.activities.WeaveBookmarksListActivity.java
org.zirco.ui.activities.preferences.BaseSpinnerCustomPreferenceActivity.java
org.zirco.ui.activities.preferences.HomepagePreferenceActivity.java
org.zirco.ui.activities.preferences.PreferencesActivity.java
org.zirco.ui.activities.preferences.SearchUrlPreferenceActivity.java
org.zirco.ui.activities.preferences.UserAgentPreferenceActivity.java
org.zirco.ui.activities.preferences.WeavePreferencesActivity.java
org.zirco.ui.activities.preferences.WeaveServerPreferenceActivity.java
org.zirco.ui.components.CustomWebViewClient.java
org.zirco.ui.components.CustomWebView.java
org.zirco.ui.runnables.DownloadRunnable.java
org.zirco.ui.runnables.FaviconUpdaterRunnable.java
org.zirco.ui.runnables.HideToolbarsRunnable.java
org.zirco.ui.runnables.HistoryUpdater.java
org.zirco.ui.runnables.XmlHistoryBookmarksExporter.java
org.zirco.ui.runnables.XmlHistoryBookmarksImporter.java
org.zirco.utils.AnimationManager.java
org.zirco.utils.ApplicationUtils.java
org.zirco.utils.Constants.java
org.zirco.utils.DateUtils.java
org.zirco.utils.IOUtils.java
org.zirco.utils.ProxyChangeReceiver.java
org.zirco.utils.ProxySettings.java
org.zirco.utils.UrlUtils.java