Changeset 61:b8ef15c8c4a5 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Nov 22, 2012, 4:38:53 PM (11 years ago)
Author:
casties
Branch:
default
Message:

implemented new shape format for image annotations.
minor cleanups.

Location:
src/main/java/de/mpiwg/itgroup/annotations/restlet
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r41 r61  
    5555        logger.debug("annotation-id=" + id);
    5656
    57        
    5857        // do authentication
    5958        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
     
    6160
    6261        if (id == null) {
    63            
    6462            return getAllAnnotations(authUser);
    6563        }
    6664
    67      
    6865        AnnotationStore store = getAnnotationStore();
    6966        Annotation annot = store.getAnnotationById(id);
    7067        if (annot != null) {
    71             if (! annot.isActionAllowed("read", authUser, store)) {
     68            if (!annot.isActionAllowed("read", authUser, store)) {
    7269                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    7370                return null;
     
    8582
    8683    private Representation getAllAnnotations(Person authUser) {
    87        
    88         Form form = getRequest().getResourceRef().getQueryAsForm();
    89            String sortBy=null;
    90            for (Parameter parameter : form) {
    91              if (parameter.getName().equals("sortBy")){
    92              sortBy = parameter.getValue();
    93              }
    94            }
    95        
     84
     85        Form form = getRequest().getResourceRef().getQueryAsForm();
     86        String sortBy = null;
     87        for (Parameter parameter : form) {
     88            if (parameter.getName().equals("sortBy")) {
     89                sortBy = parameter.getValue();
     90            }
     91        }
     92
    9693        AnnotationStore store = getAnnotationStore();
    9794        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    98        
    99         List<Annotation> annotations = store.getAnnotations(null, null);
     95
     96        List<Annotation> annotations = store.getAnnotations(null, null);
    10097        for (Annotation annotation : annotations) {
    101                  //check permission
    102                          if (!annotation.isActionAllowed("read", authUser, store)) continue;
    103      
    104                  JSONObject jo = createAnnotatorJson(annotation,false);
    105              results.add(jo);
    106            
    107             }
    108        
    109         if (sortBy!=null){
    110                 JSONObjectComparator.sortAnnotations(results,sortBy);
    111         }
    112        
    113         JSONArray resultsJa = new JSONArray();
    114         for (JSONObject result:results){
    115                 resultsJa.put(result);
    116         }
    117        
     98            // check permission
     99            if (!annotation.isActionAllowed("read", authUser, store)) continue;
     100
     101            JSONObject jo = createAnnotatorJson(annotation, false);
     102            results.add(jo);
     103        }
     104
     105        if (sortBy != null) {
     106            JSONObjectComparator.sortAnnotations(results, sortBy);
     107        }
     108
     109        JSONArray resultsJa = new JSONArray();
     110        for (JSONObject result : results) {
     111            resultsJa.put(result);
     112        }
     113
    118114        // assemble result object
    119115        JSONObject result = new JSONObject();
     
    130126    }
    131127
    132        
    133        
    134        
    135 
    136         /**
     128    /**
    137129     * POST with JSON content-type. Creates a new Annotation.
    138130     *
     
    144136        // set headers
    145137        setCorsHeaders();
    146        
    147         // do authentication TODO: who's allowed to create? 
     138
     139        // do authentication TODO: who's allowed to create?
    148140        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    149141        logger.debug("request authenticated=" + authUser);
     
    222214                return null;
    223215            }
    224             if (! storedAnnot.isActionAllowed("update", authUser, store)) {
     216            if (!storedAnnot.isActionAllowed("update", authUser, store)) {
    225217                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    226218                return null;
     
    272264        Annotation annot = store.getAnnotationById(id);
    273265        if (annot != null) {
    274             if (! annot.isActionAllowed("delete", authUser, store)) {
     266            if (!annot.isActionAllowed("delete", authUser, store)) {
    275267                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    276268                return null;
    277269            }
    278270        }
    279        
     271
    280272        // delete annotation
    281273        store.deleteAnnotationById(id);
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r58 r61  
    133133    /**
    134134     * Checks Annotator Auth plugin authentication information from headers.
    135      * Returns userId if successful. Returns "anonymous" in non-authorization mode.
     135     * Returns userId if successful. Returns "anonymous" in non-authorization
     136     * mode.
    136137     *
    137138     * @param entity
     
    184185     *
    185186     * @param annot
    186      * @param forAnonymous TODO
     187     * @param forAnonymous
     188     *            TODO
    187189     * @return
    188190     */
     
    236238                    jo.put("ranges", transformToRanges(fragments));
    237239                } else if (xt == FragmentTypes.AREA) {
    238                     jo.put("areas", transformToAreas(fragments));
    239                 }
    240             }
    241            
     240                    jo.put("shapes", transformToShapes(fragments));
     241                }
     242            }
     243
    242244            /*
    243245             * permissions
     
    282284                readPerms.put(readPerm.getIdString());
    283285            }
    284            
     286
    285287            /*
    286288             * tags
    287289             */
    288             Set<String> tagset = annot.getTags(); 
     290            Set<String> tagset = annot.getTags();
    289291            if (tagset != null) {
    290292                JSONArray tags = new JSONArray();
     
    294296                }
    295297            }
    296            
     298
    297299            /*
    298300             * id
     
    304306            return jo;
    305307        } catch (JSONException e) {
    306             // TODO Auto-generated catch block
    307             e.printStackTrace();
     308            logger.error("Unable to create AnnotatorJSON!", e);
    308309        }
    309310        return null;
     
    311312
    312313    private JSONArray transformToRanges(List<String> xpointers) {
    313 
    314314        JSONArray ja = new JSONArray();
    315 
    316315        Pattern rg = Pattern
    317316                .compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)/range-to\\(end-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)\\)");
    318317        Pattern rg1 = Pattern.compile("xpointer\\(start-point\\(string-range\\(\"([^\"]*)\",([^,]*),1\\)\\)\\)");
    319 
    320318        try {
    321319            for (String xpointer : xpointers) {
     
    323321                String decoded = xpointer;
    324322                Matcher m = rg.matcher(decoded);
    325 
    326323                if (m.find()) {
    327                     {
    328                         JSONObject jo = new JSONObject();
    329                         jo.put("start", m.group(1));
    330                         jo.put("startOffset", m.group(2));
    331                         jo.put("end", m.group(3));
    332                         jo.put("endOffset", m.group(4));
    333                         ja.put(jo);
    334                     }
     324                    JSONObject jo = new JSONObject();
     325                    jo.put("start", m.group(1));
     326                    jo.put("startOffset", m.group(2));
     327                    jo.put("end", m.group(3));
     328                    jo.put("endOffset", m.group(4));
     329                    ja.put(jo);
    335330                }
    336331                m = rg1.matcher(xpointer);
     
    339334                    jo.put("start", m.group(1));
    340335                    jo.put("startOffset", m.group(2));
    341 
    342336                    ja.put(jo);
    343337                }
    344338            }
    345339        } catch (JSONException e) {
    346             // TODO Auto-generated catch block
    347             e.printStackTrace();
     340            logger.error("Unable to transform to ranges!", e);
    348341        }
    349342        return ja;
    350343    }
    351344
    352     private JSONArray transformToAreas(List<String> xpointers) {
    353 
     345    private JSONArray transformToShapes(List<String> xpointers) {
    354346        JSONArray ja = new JSONArray();
    355 
    356         Pattern rg = Pattern.compile("xywh=(\\w*:)([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
    357 
     347        Pattern rg = Pattern.compile("xywh=(\\w*):([\\d\\.]+),([\\d\\.]+),([\\d\\.]+),([\\d\\.]+)");
    358348        try {
    359349            for (String xpointer : xpointers) {
    360                 // String decoded = URLDecoder.decode(xpointer, "utf-8");
    361350                String decoded = xpointer;
    362351                Matcher m = rg.matcher(decoded);
    363 
    364352                if (m.find()) {
    365                     {
    366                         JSONObject jo = new JSONObject();
    367                         @SuppressWarnings("unused")
    368                         String unit = m.group(1);
    369                         jo.put("x", m.group(2));
    370                         jo.put("y", m.group(3));
    371                         jo.put("width", m.group(4));
    372                         jo.put("height", m.group(5));
    373                         ja.put(jo);
     353                    String units = m.group(1);
     354                    float x = getFloat(m.group(2));
     355                    float y = getFloat(m.group(3));
     356                    float width = getFloat(m.group(4));
     357                    float height = getFloat(m.group(5));
     358                    JSONObject shape = new JSONObject();
     359                    JSONObject geom = new JSONObject();
     360                    geom.put("units", units);
     361                    geom.put("x", x);
     362                    geom.put("y", y);
     363                    if (width == 0 || height == 0) {
     364                        shape.put("type", "point");
     365                        shape.put("geometry", geom);
     366                    } else {
     367                        shape.put("type", "rectangle");
     368                        geom.put("width", width);
     369                        geom.put("height", height);
     370                        shape.put("geometry", geom);
    374371                    }
     372                    ja.put(shape);
    375373                }
    376374            }
    377375        } catch (JSONException e) {
    378             // TODO Auto-generated catch block
    379             e.printStackTrace();
     376            logger.error("Unable to transform to shapes!", e);
    380377        }
    381378        return ja;
     379    }
     380
     381    protected String parseShape(JSONObject shape) throws JSONException {
     382        String fragment = null;
     383        String type = shape.getString("type");
     384        JSONObject geom = shape.getJSONObject("geometry");
     385        if (type.equalsIgnoreCase("point")) {
     386            String x = geom.getString("x");
     387            String y = geom.getString("y");
     388            fragment = String.format("xywh=fraction:%s,%s,0,0", x, y);
     389        } else if (type.equalsIgnoreCase("point")) {
     390            String x = geom.getString("x");
     391            String y = geom.getString("y");
     392            String width = shape.getString("width");
     393            String height = shape.getString("height");
     394            fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
     395        } else {
     396            logger.error("Unable to parse this shape: " + shape);
     397        }
     398        return fragment;
    382399    }
    383400
     
    539556
    540557        /*
    541          * create xpointer from the first range/area
     558         * create fragment from the first range/area
    542559         */
    543560        try {
     
    551568            // nothing to do
    552569        }
     570        try {
     571            if (jo.has("shapes")) {
     572                JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0);
     573                annot.setFragmentType(FragmentTypes.AREA);
     574                String fragment = parseShape(shapes);
     575                annot.setTargetFragment(fragment);
     576            }
     577        } catch (JSONException e) {
     578            // nothing to do
     579        }
     580        // deprecated areas type
    553581        try {
    554582            if (jo.has("areas")) {
     
    601629        }
    602630
    603        
    604631        return annot;
    605632    }
    606633
    607     @SuppressWarnings("unused") // i in for loop
     634    @SuppressWarnings("unused")
     635    // i in for loop
    608636    protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
    609637        Actor actor = null;
     
    622650    }
    623651
     652    public float getFloat(String s) {
     653        try {
     654            return Float.parseFloat(s);
     655        } catch (NumberFormatException e) {
     656        }
     657        return 0f;
     658    }
    624659}
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java

    r50 r61  
    1818import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    1919
    20 
    2120/**
    22  * API for accessing tags in the Annotation store.
     21 * API for accessing resource objects in the Annotation store.
    2322 *
    2423 * @author dwinter
    25  *
     24 * 
    2625 */
    2726public class AnnotatorResources extends AnnotatorResourceImpl {
     
    3130
    3231    /**
    33      * GET with JSON content-type.
    34      * Parameters:
    35      *   user: short user name
    36      *   uri: user uri
    37      *   
     32     * GET with JSON content-type.
     33     * Parameters: user: short user name uri: user uri
     34     *
    3835     * @param entity
    3936     * @return
     
    4138    @Get("json")
    4239    public Representation doGetJSON(Representation entity) {
    43         logger.debug("AnnotatorGroups doGetJSON!");
     40        logger.debug("AnnotatorResources doGetJSON!");
    4441        setCorsHeaders();
    45        
     42
    4643        String jsonId = (String) getRequest().getAttributes().get("id");
    4744        String uri = decodeJsonId(jsonId);
    48        
     45
    4946        logger.debug("resources-id=" + uri);
    5047
    51         if (uri==null){
    52         return getAllResources();
     48        if (uri == null) {
     49            return getAllResources();
    5350        } else {
    54 
    55                 return getResource(uri);
     51            return getResource(uri);
    5652        }
    5753    }
    58    
    59     protected Representation getResource(String uri){
    60           AnnotationStore store = getAnnotationStore();
    61           //String tagUri=NS.MPIWG_TAGS_URL+id;
    62           Node tagNode = store.getResourceNodeByUri(uri);
    63           Resource resource = store.createResourceFromNode(tagNode);
    64           JSONObject jo = new JSONObject();
    65           try {
    66               jo.put("id", encodeJsonId(resource.getUri()));
    67               jo.put("uri", resource.getUri());
    68           } catch (JSONException e) {
    69           }
    70          
    71           return new JsonRepresentation(jo);
     54
     55    protected Representation getResource(String uri) {
     56        AnnotationStore store = getAnnotationStore();
     57        Node resNode = store.getResourceNodeByUri(uri);
     58        Resource resource = store.createResourceFromNode(resNode);
     59        JSONObject jo = new JSONObject();
     60        try {
     61            jo.put("id", encodeJsonId(resource.getUri()));
     62            jo.put("uri", resource.getUri());
     63        } catch (JSONException e) {
     64        }
     65
     66        return new JsonRepresentation(jo);
    7267    }
    73         protected Representation getAllResources() {
    74                 JSONArray results = new JSONArray();
     68
     69    protected Representation getAllResources() {
     70        JSONArray results = new JSONArray();
    7571        AnnotationStore store = getAnnotationStore();
    76        
    77        
    78             List<Resource> resources = store.getResources(null, null);
    79             for (Resource resource : resources) {
    80                 JSONObject jo = new JSONObject();
    81                 try {
    82                          jo.put("id", encodeJsonId(resource.getUri()));
    83                      jo.put("uri", resource.getUri());
    84                 } catch (JSONException e) {
    85                 }
    86                 results.put(jo);
     72
     73        List<Resource> resources = store.getResources(null, null);
     74        for (Resource resource : resources) {
     75            JSONObject jo = new JSONObject();
     76            try {
     77                jo.put("id", encodeJsonId(resource.getUri()));
     78                jo.put("uri", resource.getUri());
     79            } catch (JSONException e) {
    8780            }
    88        
     81            results.put(jo);
     82        }
     83
    8984        // assemble result object
    9085        JSONObject result = new JSONObject();
     
    9994        logger.debug(result);
    10095        return new JsonRepresentation(result);
    101         }               
    102        
     96    }
    10397
    104    
    10598}
Note: See TracChangeset for help on using the changeset viewer.