Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2010 Erik Bengtson and others. 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. Contributors: ... /
 
 package org.datanucleus.store.json;
 
 import java.net.URL;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 import  org.datanucleus.PersistenceConfiguration;
 import  org.datanucleus.store.ExecutionContext;
 import  org.datanucleus.store.FieldValues2;
 import  org.datanucleus.store.ObjectProvider;
 import  org.datanucleus.store.Type;
 
 public abstract class CloudStoragePersistenceHandler extends JsonPersistenceHandler
 {
     {
         super(storeMgr);
     }
     
     public void insertObject(ObjectProvider sm)
     {
         // Check if read-only so update not permitted
         .assertReadOnlyForUpdateOfObject(sm);
 
         Map<String,Stringoptions = new HashMap<String,String>();
         options.put(."/");
         ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
         URLConnection conn = (URLConnectionmconn.getConnection();
         createBucket(conngetHeaderForBucket());
 
         int[] fieldNumbers = sm.getClassMetaData().getAllMemberPositions();
         options.put(.getURLPath(sm));
         options.put("Content-Type""application/json");
         mconn = .getConnection(sm.getExecutionContext(), options);
         conn = (URLConnectionmconn.getConnection();
         JSONObject jsonobj = new JSONObject();
 
         sm.provideFields(fieldNumbersnew InsertFieldManager(smjsonobj));
         sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(smjsonobj));
 
         write("PUT",conn.getURL().getPath(),conn,jsonobjgetHeaders("PUT",options));
     }
 
     protected void createBucket(URLConnection connMap headers)
     {
         try
         {
             if (..isDebugEnabled())
             {
                 ..debug("Creating bucket. ");
             }
            HttpURLConnection http = (HttpURLConnection)conn;
            Iterator iterator = headers.keySet().iterator();
            while(iterator.hasNext())
            {
                String key = (Stringiterator.next();
                String value = (Stringheaders.get(key);
                http.setRequestProperty(keyvalue);
            }
            http.setRequestProperty("Content-Length""0");
            http.setDoOutput(true);
            http.setRequestMethod("PUT");
            http.setReadTimeout(10000);
            http.setConnectTimeout(10000);
            http.connect();
            int code = http.getResponseCode();
            
            if(code>=400)
            {
                StringBuffer sb = new StringBuffer();
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int r;
                    while ((r = http.getErrorStream().read(buffer)) != -1) {
                        baos.write(buffer, 0, r);
                    }
                    
                    sb.append(new String(baos.toByteArray()));        
                http.getErrorStream().close();
                throw new NucleusDataStoreException("HTTP Error code: "+code+" "+http.getResponseMessage()+" error: "+sb.toString());
            }
            if(code>=300)
            {
                throw new NucleusDataStoreException("Redirect not supported. HTTP Error code: "+code+" "+http.getResponseMessage());
            }
        }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    
    protected Map<StringStringgetHeaders(String httpVerbMap<String,Stringoptions)
    {
        Map<StringStringheaders = super.getHeaders(httpVerboptions);
        String contentMD5 = "";
        String contentType = "";
        if (options.containsKey("Content-Type"))
        {
            contentType = options.get("Content-Type");
        }
        String urlStr = .getConnectionURL();
        String authenticationKey = .getConnectionUserName();
        String authenticationSecretKey = .getConnectionPassword();
        try
        {
            urlStr = urlStr.substring(urlStr.indexOf(.getStoreManagerKey()+":")+.getStoreManagerKey().length()+1);
            headers.put("Host"getBucket()+"."+new URL(urlStr).getHost());
            String path = "/"+getBucket();
            if(options.containsKey(.))
            {
                if(!options.get(.).toString().startsWith("/"))
                {
                    path += "/";
                }
                if(options.get(.).toString().indexOf("?")>-1)
                {
                    path += options.get(.).toString().substring(0,options.get(.).toString().indexOf("?"));
                }
                else
                {
                    path += options.get(.).toString();
                }
            }
            else
            {
                path += new URL(urlStr).getPath();
            }
            String stringToSign = httpVerb + "\n" + contentMD5 + "\n" + contentType + "\n" + headers.get("Date") + "\n" + path;
            headers.put("Authorization"getRealmName()+" "+authenticationKey+":"+CloudStorageUtils.hmac(authenticationSecretKey,stringToSign));
        }
        catch (MalformedURLException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
        return headers;
    }
    
    protected abstract String getRealmName();
    
    protected Map getHeaderForBucket()
    {
        Map headers = new HashMap();
        headers.put("Date", CloudStorageUtils.getHTTPDate());
        String contentMD5 = "";
        String contentType = "";
        String httpVerb = "PUT";
        String urlStr = .getConnectionURL();
        String awsKey = .getConnectionUserName();
        String awsSecretKey = .getConnectionPassword();
        try
        {
            urlStr = urlStr.substring(urlStr.indexOf(.getStoreManagerKey()+":")+.getStoreManagerKey().length()+1);
            headers.put("Host"getBucket()+"."+new URL(urlStr).getHost());
            String stringToSign = httpVerb + "\n" + contentMD5 + "\n" + contentType + "\n" + headers.get("Date") + "\n" + "/"+getBucket() +"/";
            headers.put("Authorization""AWS "+awsKey+":"+CloudStorageUtils.hmac(awsSecretKey,stringToSign));
        }
        catch (MalformedURLException e)
        {
            e.printStackTrace();
        }
        return headers;
    }    

    
Convenience method to get all objects of the candidate type from the specified connection.

Parameters:
om ObjectManager
mconn Managed Connection
candidateClass Candidate
ignoreCache Whether to ignore the cache
Returns:
List of objects of the candidate type
    public List getObjectsOfCandidateType(ExecutionContext omManagedConnection mconn
            Class candidateClassboolean ignoreCacheMap options)
    {
        List results = new ArrayList();
        try
        {
            URLConnection conn = (URLConnectionmconn.getConnection();
            ClassLoaderResolver clr = om.getClassLoaderResolver();
            final AbstractClassMetaData cmd = om.getMetaDataManager().getMetaDataForClass(candidateClassclr);
            JSONArray jsonarray;
            try
            {                
                HttpURLConnection http = (HttpURLConnectionconn;
                Map headers = getHeaders("GET"options);
                Iterator iterator = headers.keySet().iterator();
                while(iterator.hasNext())
                {
                    String key = (Stringiterator.next();
                    String value = (Stringheaders.get(key);
                    http.setRequestProperty(keyvalue);
                }
                http.setDoInput(true);
                http.setRequestMethod("GET");
                http.setReadTimeout(10000);
                http.setConnectTimeout(10000);
                http.connect();
                int code = http.getResponseCode();
                if (code == 404)
                {
                    return .;
                }
                handleHTTPErrorCode(http);
                StringBuffer sb = new StringBuffer();
                if (http.getContentLength() > 0)
                {
                    for (int i = 0; i < http.getContentLength(); i++)
                    {
                        sb.append((charhttp.getInputStream().read());
                    }
                }
                else
                {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int r;
                    while ((r = http.getInputStream().read(buffer)) != -1)
                    {
                        baos.write(buffer, 0, r);
                    }
                    sb.append(new String(baos.toByteArray()));
                }
                http.getInputStream().close();
                String contentType = http.getHeaderField("content-type");
                //content-type = application/xml; charset=UTF-8  (charset is optional)
                if(contentType!=null && (contentType.split(";")[0].equalsIgnoreCase("application/xml") || contentType.split(";")[0].equalsIgnoreCase("text/xml")) )
                {
                    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(sb.toString().getBytes()));
                    NodeList list = doc.getElementsByTagName("Contents");
                    Set set = new HashSet();
                    for (int i=0; i<list.getLength(); i++)
                    {
                        Element el = (Elementlist.item(i);
                        JSONObject object = new JSONObject();
                        String keyText = el.getElementsByTagName("Key").item(0).getTextContent();
                        if(keyText.indexOf("/") < 1)
                        {
                            //log ignoring this element
                        }
                        else
                        {
                            String key = keyText.substring(keyText.indexOf("/",1)+1);
                            if(key.length()<1)
                            {
                                //log ignoring this element
                            }
                            else
                            {
                                String className = keyText.substring(0,keyText.indexOf("/"));
                                object.put("class"className);
                                object.put(cmd.getPrimaryKeyMemberNames()[0], key);
                                set.add(object);
                            }
                        }
                    }
                    jsonarray = new JSONArray(set);
                }
                else
                {
                    jsonarray = new JSONArray(sb.toString());
                }
            }
            catch (IOException e)
            {
                throw new NucleusDataStoreException(e.getMessage(), e);
            }
            catch (JSONException e)
            {
                throw new NucleusDataStoreException(e.getMessage(), e);
            }
            catch (SAXException e)
            {
                throw new NucleusDataStoreException(e.getMessage(), e);
            }
            catch (ParserConfigurationException e)
            {
                throw new NucleusDataStoreException(e.getMessage(), e);
            }
            for (int i = 0; i < jsonarray.length(); i++)
            {
                final JSONObject json = jsonarray.getJSONObject(i);
                results.add(om.findObjectUsingAID(new Type(clr.classForName(cmd.getFullClassName())), new FieldValues2()
                {
                    // StateManager calls the fetchFields method
                    public void fetchFields(ObjectProvider sm)
                    {
                        //we only replace the primary key, since it was the only field loaded
                        sm.replaceFields(cmd.getPKMemberPositions(), new FetchFieldManager(smjson));
                    }
                    public void fetchNonLoadedFields(ObjectProvider sm)
                    {
                        //we only replace the primary key, since it was the only field loaded
                        sm.replaceNonLoadedFields(cmd.getPKMemberPositions(), new FetchFieldManager(smjson));
                    }
                    public FetchPlan getFetchPlanForLoading()
                    {
                        return null;
                    }
                }, ignoreCachetrue));
            }
        }
        catch (JSONException je)
        {
            je.printStackTrace();
        }
        return results;
    }

    
URL path for querying in the cloud storage. it lists all entries for the bucket+a prefix
    {
        String url = acmd.getValueForExtension("url");
        if (url==null)
        {
            url = acmd.getFullClassName();
        }        
        url = "?prefix="+url;
        return url;
    }
    
    private String getBucket()
    {
        PersistenceConfiguration conf = .getOMFContext().getPersistenceConfiguration();
        return conf.getStringProperty("datanucleus.cloud.storage.bucket");
    }
New to GrepCode? Check out our FAQ X