Ignore:
Timestamp:
Jul 3, 2012, 7:23:17 PM (12 years ago)
Author:
casties
Branch:
default
Message:

version 0.2 really works now

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

Legend:

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

    r4 r5  
    99import java.security.InvalidKeyException;
    1010import java.security.SignatureException;
     11import java.text.SimpleDateFormat;
    1112import java.util.ArrayList;
     13import java.util.Calendar;
    1214import java.util.List;
    1315import java.util.regex.Matcher;
     
    178180     */
    179181    public JSONObject createAnnotatorJson(Annotation annot) {
     182        // return user as a JSON object (otherwise just as string)
    180183        boolean makeUserObject = true;
    181184        JSONObject jo = new JSONObject();
     
    198201                userObject.put("id", userId);
    199202                // get full name
    200                 RestServer restServer = (RestServer) getApplication();
    201                 String userName = restServer.getUserNameFromLdap(userId);
     203                String userName = annot.getCreatorName();
     204                if (userName == null) {
     205                    RestServer restServer = (RestServer) getApplication();
     206                    userName = restServer.getFullNameFromLdap(userId);
     207                }
    202208                userObject.put("name", userName);
    203209                // save user object
     
    241247        try {
    242248            for (String xpointer : xpointers) {
    243                 String decoded = URLDecoder.decode(xpointer, "utf-8");
     249                //String decoded = URLDecoder.decode(xpointer, "utf-8");
     250                String decoded = xpointer;
    244251                Matcher m = rg.matcher(decoded);
    245252
     
    266273            // TODO Auto-generated catch block
    267274            e.printStackTrace();
    268         } catch (UnsupportedEncodingException e) {
    269             // TODO Auto-generated catch block
    270             e.printStackTrace();
    271         }
    272 
     275        }
    273276        return ja;
    274277    }
     
    282285        try {
    283286            for (String xpointer : xpointers) {
    284                 String decoded = URLDecoder.decode(xpointer, "utf-8");
     287                //String decoded = URLDecoder.decode(xpointer, "utf-8");
     288                String decoded = xpointer;
    285289                Matcher m = rg.matcher(decoded);
    286290
     
    300304            // TODO Auto-generated catch block
    301305            e.printStackTrace();
    302         } catch (UnsupportedEncodingException e) {
    303             // TODO Auto-generated catch block
    304             e.printStackTrace();
    305         }
    306 
     306        }
    307307        return ja;
    308308    }
    309309
    310     protected String parseArea(JSONObject area) throws JSONException, UnsupportedEncodingException {
     310    protected String parseArea(JSONObject area) throws JSONException {
    311311        String x = area.getString("x");
    312312        String y = area.getString("y");
     
    317317            height = area.getString("height");
    318318        }
    319         String fragment = URLEncoder.encode(String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height), "utf-8");
     319        String fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height);
    320320        return fragment;
    321321    }
    322322
    323     protected String parseRange(JSONObject range) throws JSONException, UnsupportedEncodingException {
     323    protected String parseRange(JSONObject range) throws JSONException {
    324324        String start = range.getString("start");
    325325        String end = range.getString("end");
     
    327327        String endOffset = range.getString("endOffset");
    328328
    329         String fragment = URLEncoder.encode(String.format(
     329        String fragment = String.format(
    330330                "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start,
    331                 startOffset, end, endOffset), "utf-8");
     331                startOffset, end, endOffset);
    332332        return fragment;
    333333    }
    334334
    335335    /**
    336      * creates an Annotation object with data from JSON.
     336     * Creates an Annotation object with data from JSON.
    337337     *
    338338     * uses the specification from the annotator project: {@link https
     
    352352    }
    353353
     354    /**
     355     * Updates an Annotation object with data from JSON.
     356     *
     357     * uses the specification from the annotator project: {@link https
     358     * ://github.com/okfn/annotator/wiki/Annotation-format}
     359     *
     360     * The username will be transformed to an URI if not given already as URI,
     361     * if not it will set to the MPIWG namespace defined in
     362     * de.mpiwg.itgroup.annotationManager.Constants.NS
     363     *
     364     * @param annot
     365     * @param jo
     366     * @return
     367     * @throws JSONException
     368     * @throws UnsupportedEncodingException
     369     */
    354370    public Annotation updateAnnotation(Annotation annot, JSONObject jo, Representation entity) throws JSONException,
    355371            UnsupportedEncodingException {
    356         // annotated uri
     372        // target uri
    357373        if (jo.has("uri")) {
    358374            annot.setTargetBaseUri(jo.getString("uri"));
     
    397413            username = authUser;
    398414        }
    399         // username should be a URI, if not it will set to the MPIWG namespace
    400         // defined in
    401         // de.mpiwg.itgroup.annotationManager.Constants.NS
     415        // try to get full name
     416        if (username != null) {
     417            RestServer restServer = (RestServer) getApplication();
     418            String fullName = restServer.getFullNameFromLdap(username);
     419            annot.setCreatorName(fullName);
     420        }
     421        // userUri should be a URI, if not it will set to the MPIWG namespace
    402422        if (userUri == null) {
    403423            if (username.startsWith("http")) {
     
    408428        }
    409429        // TODO: should we overwrite the creator?
     430        if (annot.getCreatorUri() == null) {
     431            annot.setCreatorUri(userUri);
     432        }
     433       
     434        if (annot.getCreated() == null) {
     435            // set creation date
     436            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
     437            String ct = format.format(Calendar.getInstance().getTime());
     438            annot.setCreated(ct);
     439        }
    410440
    411441        // create xpointer from the first range/area
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java

    r4 r5  
    4141    public String CONSUMER_KEYS_PATH = "WEB-INF/consumerkeys.property";
    4242
     43    private Properties serverConfig;
     44    public String CONFIG_PROPS_PATH = "WEB-INF/serverconfig.property";
     45
    4346    private GraphDatabaseService graphDb;
    4447    public static final String GRAPHDB_KEY = "annotationmanager.graphdb";
    45     public String DB_PATH = "WEB-INF/neo4j-annotation-db";
     48    public static final String GRAPHDB_PATH_KEY = "annotationmanager.graphdb.path";
     49    public String graphdbPath = "WEB-INF/neo4j-annotation-db";
    4650
    4751    private WrappingNeoServerBootstrapper srv;
     
    6468                .get("org.restlet.ext.servlet.ServletContext");
    6569        if (sc != null) {
     70            /*
     71             * read config from webapp
     72             */
     73            serverConfig = new Properties();
     74            InputStream ps1 = getResourceAsStream(sc, CONFIG_PROPS_PATH);
     75            if (ps1 != null) {
     76                logger.debug("loading config from " + CONFIG_PROPS_PATH);
     77                try {
     78                    serverConfig.load(ps1);
     79                    /*
     80                     * read serverconfig options
     81                     */
     82                    graphdbPath = serverConfig.getProperty(GRAPHDB_PATH_KEY, graphdbPath);
     83                } catch (IOException e) {
     84                    logger.warn("Error loading server config: ", e);
     85                }
     86                logger.debug("config: " + serverConfig);
     87            } else {
     88                logger.error("Unable to get resource " + CONFIG_PROPS_PATH);
     89            }
    6690            // look for database service in context
    6791            graphDb = (GraphDatabaseService) sc.getAttribute(GRAPHDB_KEY);
     
    7094                 * open database
    7195                 */
    72                 String dbFn = getResourcePath(sc, DB_PATH);
     96                String dbFn = getResourcePath(sc, graphdbPath);
    7397                if (dbFn != null) {
    7498                    logger.debug("opening DB " + dbFn);
     
    87111                    srv.start();
    88112                } else {
    89                     logger.error("Unable to get resource " + DB_PATH);
     113                    logger.error("Unable to get resource " + dbFn);
    90114                }
    91115            }
     
    94118             */
    95119            consumerKeys = new Properties();
    96             InputStream ps = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
    97             if (ps != null) {
     120            InputStream ps2 = getResourceAsStream(sc, CONSUMER_KEYS_PATH);
     121            if (ps2 != null) {
    98122                logger.debug("loading consumer keys from " + CONSUMER_KEYS_PATH);
    99123                try {
    100                     consumerKeys.load(ps);
     124                    consumerKeys.load(ps2);
    101125                } catch (IOException e) {
    102126                    // TODO Auto-generated catch block
     
    155179     * @return
    156180     */
    157     public String getUserNameFromLdap(String creator) {
     181    public String getFullNameFromLdap(String creator) {
    158182        String retString = creator; // falls nichts gefunden wird einfach den
    159183                                    // creator zurueckgeben
     
    163187
    164188        // TODO: should go into config file
    165         String ldapUrl = "ldap://ldapreplik.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";
     189        String ldapUrl = "ldap://ldap.mpiwg-berlin.mpg.de/dc=mpiwg-berlin,dc=mpg,dc=de";
    166190        env.put(javax.naming.Context.PROVIDER_URL, ldapUrl);
    167191
     
    175199        }
    176200
    177         String base = "ou=People";
     201        String base = "ou=people";
    178202
    179203        SearchControls sc = new SearchControls();
Note: See TracChangeset for help on using the changeset viewer.