001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019package org.apache.isis.viewer.restfulobjects.server.util;
020
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.apache.isis.core.metamodel.adapter.oid.OidMarshaller;
025import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
026
027public final class UrlParserUtils {
028
029    private final static Pattern OBJECT_OID = Pattern.compile(".*objects\\/([^/]+)\\/(.+)");;
030    private final static Pattern DOMAIN_TYPE = Pattern.compile(".*domain-types\\/([^/]+).*");;
031
032    public final static String encodedOidFromLink(final JsonRepresentation link) {
033        final String href = link.getString("href");
034        
035        final Matcher matcher = OBJECT_OID.matcher(href);
036        if (!matcher.matches()) {
037            return null;
038        }
039        String domainType = matcher.group(1);
040        String instanceId = matcher.group(2);
041        return getOidMarshaller().joinAsOid(domainType, instanceId);
042    }
043
044    public final static String domainTypeFrom(final JsonRepresentation link) {
045        return domainTypeFrom(link.getString("href"));
046    }
047
048    public static String domainTypeFrom(final String href) {
049        final Matcher matcher = DOMAIN_TYPE.matcher(href);
050        if (!matcher.matches()) {
051            return null;
052        }
053        return matcher.group(1);
054    }
055
056    private static OidMarshaller getOidMarshaller() {
057        return new OidMarshaller();
058    }
059
060}