Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009 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.ObjectManager;
 import  org.datanucleus.PersistenceConfiguration;
 import  org.datanucleus.StateManager;
 
 {
     {
         super(storeMgr);
     }
     
     public void insertObject(StateManager sm)
     {
         // Check if read-only so update not permitted
         .assertReadOnlyForUpdateOfObject(sm);
 
         Properties options = new Properties();
         options.put(."/");
         ManagedConnection mconn = .getConnection(sm.getObjectManager(), options);
         URLConnection conn = (URLConnectionmconn.getConnection();
         createBucket(conngetHeaderForBucket());
 
         int[] fieldNumbers = sm.getClassMetaData().getAllMemberPositions();
         options.put(.getURLPath(sm));
         mconn = .getConnection(sm.getObjectManager(), 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 getHeaders(String httpVerbMap options)
    {
        Map headers = new HashMap();
        headers.put("Date", AmazonS3Utils.getHTTPDate());
        String contentMD5 = "";
        String contentType = "";
        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 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""AWS "+awsKey+":"+AmazonS3Utils.hmac(awsSecretKey,stringToSign));
        }
        catch (MalformedURLException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
        return headers;
    }
    
    protected Map getHeaderForBucket()
    {
        Map headers = new HashMap();
        headers.put("Date", AmazonS3Utils.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+":"+AmazonS3Utils.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(ObjectManager 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");
                if(contentType!=null && (contentType.equalsIgnoreCase("application/xml") || contentType.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(clr.classForName(cmd.getFullClassName()), new FieldValues()
                {
                    // StateManager calls the fetchFields method
                    public void fetchFields(StateManager sm)
                    {
                        //we only replace the primary key, since it was the only field loaded
                        sm.replaceFields(cmd.getPKMemberPositions(), new FetchFieldManager(smjson));
                    }
                    public void fetchNonLoadedFields(StateManager 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 amazon. 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.json.amazons3.bucket");
    }
New to GrepCode? Check out our FAQ X