Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 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.util.List;
 import java.util.Map;
 
 import  org.datanucleus.store.ExecutionContext;
 import  org.datanucleus.store.FieldValues2;
 import  org.datanucleus.store.ObjectProvider;
 import  org.datanucleus.store.Type;
 
 {
     protected AbstractStoreManager storeMgr;
 
     {
         this. = (AbstractStoreManager)storeMgr;
     }
     
     public void close()
     {
         // nothing to do
     }
 
     public String getURLPath(ObjectProvider sm)
     {
         String url = getURLPath(sm.getClassMetaData());
         JSONObject jsonobj = new JSONObject();
         sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(sm,jsonobj));
         try
         {
             url += jsonobj.get(JSONObject.getNames(jsonobj)[0]).toString();
         }
         catch (JSONException e)
         {
             throw new NucleusException(e.getMessage(), e);
         }
 
         return url;
     }
     
     public String getURLPath(AbstractClassMetaData acmd)
     {
         String url = acmd.getValueForExtension("url");
         if (url==null)
         {
             url = acmd.getFullClassName();
         }        
         if (!url.endsWith("/"))
         {
             url += "/";
         }
         return url;
     } 
     
     {
        String url = acmd.getValueForExtension("url");
        if (url==null)
        {
            url = acmd.getFullClassName();
        }        
        if (!url.endsWith("/"))
        {
            url += "/";
        }
        return url;
    }     
    
    public void insertObject(ObjectProvider sm)
    {
        // Check if read-only so update not permitted
        .assertReadOnlyForUpdateOfObject(sm);
        int[] fieldNumbers = sm.getClassMetaData().getAllMemberPositions();
        Map<String,Stringoptions = new HashMap<String,String>();
        options.put(.getURLPath(sm));
        options.put("Content-Type""application/json");
        ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(fieldNumbersnew InsertFieldManager(smjsonobj));
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(smjsonobj));
        write("POST",conn.getURL().toExternalForm(),conn,jsonobj,getHeaders("POST",options));
    }
    public void updateObject(ObjectProvider smint[] fieldNumbers)
    {
        // Check if read-only so update not permitted
        .assertReadOnlyForUpdateOfObject(sm);
        Map<String,Stringoptions = new HashMap<String,String>();
        options.put(.getURLPath(sm));
        options.put("Content-Type""application/json");        
        ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(fieldNumbersnew InsertFieldManager(smjsonobj));
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(smjsonobj));
        write("PUT",conn.getURL().toExternalForm(),conn,jsonobj,getHeaders("PUT",options));
    }
    public void deleteObject(ObjectProvider sm)
    {
        // Check if read-only so update not permitted
        .assertReadOnlyForUpdateOfObject(sm);
        Map<String,Stringoptions = new HashMap<String,String>();
        options.put(.getURLPath(sm));
        ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        try
        {
            HttpURLConnection http = (HttpURLConnection)conn;
            Map<String,Stringheaders = getHeaders("DELETE",options);
            Iterator iterator = headers.keySet().iterator();
            while(iterator.hasNext())
            {
                String key = (Stringiterator.next();
                String value = (Stringheaders.get(key);
                http.setRequestProperty(keyvalue);
            }
            http.setRequestMethod("DELETE");
            http.setReadTimeout(10000);
            http.setConnectTimeout(10000);
            http.connect();
            int code = http.getResponseCode();
            if (code == 404)
            {
                throw new NucleusObjectNotFoundException();
            }
            handleHTTPErrorCode(http);
         }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    protected void handleHTTPErrorCode(HttpURLConnection httpthrows IOException
    {
        if(http.getResponseCode()>=400)
        {
            StringBuffer sb = new StringBuffer();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int r;
            if (http.getErrorStream()!=null)
            {
                while ((r = http.getErrorStream().read(buffer)) != -1) {
                    baos.write(buffer, 0, r);
                }
                sb.append(new String(baos.toByteArray()));        
                http.getErrorStream().close();
            }
            throw new NucleusDataStoreException("Error on URL: '"+http.getURL().toExternalForm()+"' Request Method: "+http.getRequestMethod()+" HTTP Error code: "+http.getResponseCode()+" "+http.getResponseMessage()+" error: "+sb.toString());
        }
        if(http.getResponseCode()>=300)
        {
            throw new NucleusDataStoreException("Redirect not supported. HTTP Error code: "+http.getResponseCode()+" "+http.getResponseMessage());
        }
    }
    
    public void fetchObject(ObjectProvider smint[] fieldNumbers)
    {
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(sm,jsonobj));
        Map<String,Stringoptions = new HashMap<String,String>();
        options.put(.getURLPath(sm));
        ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        JSONObject result = read("GET",conn.getURL().toExternalForm(),conn,getHeaders("GET",options));
        
        sm.replaceFields(fieldNumbersnew FetchFieldManager(sm,result));
    }
    public Object findObject(ExecutionContext omObject id)
    {
        // TODO Auto-generated method stub
        return null;
    }
    public void locateObject(ObjectProvider sm)
    {
        Map<String,Stringoptions = new HashMap<String,String>();
        options.put(.getURLPath(sm));
        ManagedConnection mconn = .getConnection(sm.getExecutionContext(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        try
        {
            HttpURLConnection http = (HttpURLConnection)conn;
            Map<String,Stringheaders = getHeaders("HEAD",options);
            Iterator iterator = headers.keySet().iterator();
            while(iterator.hasNext())
            {
                String key = (Stringiterator.next();
                String value = (Stringheaders.get(key);
                http.setRequestProperty(keyvalue);
            }
            http.setDoOutput(true);
            http.setRequestMethod("HEAD");
            http.setReadTimeout(10000);
            http.setConnectTimeout(10000);
            http.connect();
            int code = http.getResponseCode();
            if (code == 404)
            {
                throw new NucleusObjectNotFoundException();
            }
            handleHTTPErrorCode(http);
        }
        catch (IOException e)
        {
            throw new NucleusObjectNotFoundException(e.getMessage(),e);
        }
    }
    protected void write(String methodString requestUriURLConnection connJSONObject jsonobjMap<StringStringheaders)
    {
        try
        {
            if (..isDebugEnabled())
            {
                ..debug("Writing to URL "+requestUri+" content "+jsonobj.toString());
            }
            int length = jsonobj.toString().length();
            HttpURLConnection http = (HttpURLConnection)conn;
            Iterator<Stringiterator = headers.keySet().iterator();
            while(iterator.hasNext())
            {
                String key = iterator.next();
                String value = headers.get(key);
                http.setRequestProperty(keyvalue);
            }
            http.setRequestProperty("Content-Length"""+length);
            http.setDoOutput(true);
            http.setRequestMethod(method);
            http.setReadTimeout(10000);
            http.setConnectTimeout(10000);
            http.connect();
            OutputStream os = conn.getOutputStream();
            os.write(jsonobj.toString().getBytes());
            os.flush();
            os.close();
            handleHTTPErrorCode(http);
       }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    protected JSONObject read(String methodString requestUriURLConnection connMap headers)
    {
        try
        {
            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.setDoOutput(true);
            http.setDoInput(true);
            http.setRequestMethod(method);
            http.setReadTimeout(10000);
            http.setConnectTimeout(10000);
            http.connect();
            /*
            OutputStream os = conn.getOutputStream();
            os.write(json.toString().getBytes());
            os.flush();
            os.close();
            */
            int code = http.getResponseCode();
            if (code == 404)
            {
                throw new NucleusObjectNotFoundException();
            }
            /*String msg =*/ http.getResponseMessage();
            StringBuffer sb = new StringBuffer();
            if (http.getContentLength()>0)
            {
                forint i=0; i<http.getContentLength(); i++)
                {
                    sb.append((char)http.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();
            return new JSONObject(sb.toString());
        }
        catch (SocketTimeoutException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
        catch (JSONException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    
    protected Map<String,StringgetHeaders(String httpVerbMap<String,Stringoptions)
    {
        Map<StringStringheaders = new HashMap<StringString>();
        headers.put("Date", CloudStorageUtils.getHTTPDate());
        String contentType = "";
        if (options.containsKey("Content-Type"))
        {
            contentType = options.get("Content-Type");
            headers.put("Content-Type"contentType);
        }
        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 .;
                }
                /* String msg = */http.getResponseMessage();
                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();
                jsonarray = new JSONArray(sb.toString());
            }
            catch (IOException e)
            {
                throw new NucleusDataStoreException(e.getMessage(), e);
            }
            catch (JSONException 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)
                    {
                        sm.replaceFields(cmd.getAllMemberPositions(), new FetchFieldManager(smjson));
                    }
                    public void fetchNonLoadedFields(ObjectProvider sm)
                    {
                        sm.replaceNonLoadedFields(cmd.getAllMemberPositions(), new FetchFieldManager(smjson));
                    }
                    public FetchPlan getFetchPlanForLoading()
                    {
                        return null;
                    }
                }, ignoreCachetrue));
            }
        }
        catch (JSONException je)
        {
            throw new NucleusException(je.getMessage(), je);
        }
        return results;
    }
New to GrepCode? Check out our FAQ X