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;
 
 
 
 {
     private JsonStoreManager storeMgr;
 
     {
         this. = (JsonStoreManager)storeMgr;
     }
     
     public void close()
     {
         // TODO Auto-generated method stub
     }
 
     public void insertObject(StateManager sm)
     {
         // Check if read-only so update not permitted
 
         int[] fieldNumbers = sm.getClassMetaData().getAllMemberPositions();
         String url = sm.getClassMetaData().getValueForExtension("url");
         Properties options = new Properties();
         if (url!=null)
         {
             options.put(.url);
         }
         else
         {
             options.put(.sm.getClassMetaData().getFullClassName());
         }        
         ManagedConnection mconn = getConnection(sm.getObjectManager(), 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);
     }
 
     public void updateObject(StateManager smint[] fieldNumbers)
     {
         // Check if read-only so update not permitted
 
         String url = sm.getClassMetaData().getValueForExtension("url");
         Properties options = new Properties();
         if (url!=null)
         {
             options.put(.url);
         }
         else
         {
             options.put(.sm.getClassMetaData().getFullClassName());
         }        
         ManagedConnection mconn = getConnection(sm.getObjectManager(), 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);
    }
    public void deleteObject(StateManager sm)
    {
        // Check if read-only so update not permitted
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(sm,jsonobj));
        String url = sm.getClassMetaData().getValueForExtension("url");
        try
        {
            if (url==null)
            {
                url = "";
            }
            url += jsonobj.get(JSONObject.getNames(jsonobj)[0]).toString();
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
        Properties options = new Properties();
        if (url!=null)
        {
            options.put(.url);
        }
        else
        {
        }        
        ManagedConnection mconn = getConnection(sm.getObjectManager(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        try
        {
            int length = jsonobj.toString().length();
            HttpURLConnection http = (HttpURLConnection)conn;
            http.setRequestProperty("Content-Length"""+length);
            http.setRequestMethod("DELETE");
            http.setReadTimeout(10000);
            http.connect();
            http.getResponseCode();
        }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    public void fetchObject(StateManager smint[] fieldNumbers)
    {
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(sm,jsonobj));
        String url = sm.getClassMetaData().getValueForExtension("url");
        try
        {
            if (url==null)
            {
                url = "";
            }
            url += jsonobj.get(JSONObject.getNames(jsonobj)[0]).toString();
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
        Properties options = new Properties();
        if (url!=null)
        {
            options.put(.url);
        }
        else
        {
        }        
        ManagedConnection mconn = getConnection(sm.getObjectManager(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        JSONObject result = read("GET",conn.getURL().toExternalForm(),conn,jsonobj);
        
        sm.replaceFields(fieldNumbersnew FetchFieldManager(sm,result));
    }
    public Object findObject(ObjectManager omObject id)
    {
        // TODO Auto-generated method stub
        return null;
    }
    public void locateObject(StateManager sm)
    {
        JSONObject jsonobj = new JSONObject();
        sm.provideFields(sm.getClassMetaData().getPKMemberPositions(), new InsertFieldManager(sm,jsonobj));
        String url = sm.getClassMetaData().getValueForExtension("url");
        try
        {
            if (url==null)
            {
                url = "";
            }
            url += jsonobj.get(JSONObject.getNames(jsonobj)[0]).toString();
        }
        catch (JSONException e)
        {
            throw new NucleusException(e.getMessage(), e);
        }
        Properties options = new Properties();
        if (url!=null)
        {
            options.put(.url);
        }
        else
        {
        }        
        ManagedConnection mconn = getConnection(sm.getObjectManager(), options);
        URLConnection conn = (URLConnectionmconn.getConnection();
        try
        {
            HttpURLConnection http = (HttpURLConnection)conn;
            http.setDoOutput(true);
            http.setRequestMethod("HEAD");
            http.setReadTimeout(10000);
            http.connect();
            int code = http.getResponseCode();
            if (code == 404)
            {
                throw new NucleusObjectNotFoundException();
            }
        }
        catch (IOException e)
        {
            throw new NucleusObjectNotFoundException(e.getMessage(),e);
        }
    }

    
Helper method to obtain connections

Parameters:
om
options
Returns:
    {
        return factory.createManagedConnection(omoptions);
    }
    
    private void write(String methodString requestUriURLConnection connJSONObject jsonobj)
    {
        try
        {
            if (..isDebugEnabled())
            {
                ..debug("Writing to URL "+requestUri+" content "+jsonobj.toString());
            }
            int length = jsonobj.toString().length();
            HttpURLConnection http = (HttpURLConnection)conn;
            http.setRequestProperty("Content-Length"""+length);
            http.setDoOutput(true);
            http.setRequestMethod(method);
            http.setReadTimeout(10000);
            http.connect();
            OutputStream os = conn.getOutputStream();
            os.write(jsonobj.toString().getBytes());
            os.flush();
            os.close();
            http.getResponseCode();
        }
        catch (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
    private JSONObject read(String methodString requestUriURLConnection connJSONObject jsonobj)
    {
        try
        {
            int length = jsonobj.toString().length();
            HttpURLConnection http = (HttpURLConnection)conn;
            http.setRequestProperty("Content-Length"""+length);
            //http.setDoOutput(true);
            http.setDoInput(true);
            http.setRequestMethod(method);
            http.setReadTimeout(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 (IOException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
        catch (JSONException e)
        {
            throw new NucleusDataStoreException(e.getMessage(),e);
        }
    }
New to GrepCode? Check out our FAQ X