Java Web Services via REST bereitstellen
ulrich
2017-04-25 66952711b18475fd6d1e90d767b61002b222ba98
Steuerung des Zugriffs per Annotation
1 files added
1 files modified
69 ■■■■ changed files
src/de/uhilger/transit/Access.java 30 ●●●●● patch | view | raw | blame | history
src/de/uhilger/transit/JavaServer.java 39 ●●●●● patch | view | raw | blame | history
src/de/uhilger/transit/Access.java
New file
@@ -0,0 +1,30 @@
package de.uhilger.transit;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
 * Annotation to denote access for a method.
 *
 * Transit will allow access for methods with Access.Type.ALLOW and
 * for methods without an Access annotation
 *
 * Methods with an Access.Type.RESTRICT annotation are not accessible
 * via Transit
 *
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
@Target( { METHOD } )
@Retention( RetentionPolicy.RUNTIME )
public @interface Access {
  /** API method access types as distinguished by this interface */
    enum Type{ALLOW, RESTRICT}
    /** type of database column */
    Type type() default Type.ALLOW;
}
src/de/uhilger/transit/JavaServer.java
@@ -106,7 +106,7 @@
        if(o != null) {
            Class c = o.getClass();
            Method m = findeMethode(c.getDeclaredMethods(), methodenName);
            resultat = methodeAusfuehren(o, m, format, parameter);
      resultat = methodeAusfuehren(o, m, format, parameter);
        }
        return resultat;
    }
@@ -124,23 +124,28 @@
    public Object methodeAusfuehren(Object o, Method methode, String format, Object... parameter) throws Exception {
        Object resultat = null;
        if(methode != null) {
            resultat = methode.invoke(o, parameterKonvertieren(methode, parameter, format));
            Class returnType = methode.getReturnType();
            /*
             * Wenn der Rueckgabewert der Methode nicht vom Typ String ist,
             * wird das Objekt in das Format verwandelt, das der Standardwandler
             * liefert, z.B. JSON
             */
            if(!returnType.getName().equals("java.lang.String")) {
        //resultat = new JsonWandler().vonJava(resultat, returnType);
        Wandler w = wandler.get(format);
        if(w != null) {
                resultat = w.vonJava(resultat, returnType);
      Access access = methode.getAnnotation(Access.class);
      if(access != null && access.type().equals(Access.Type.ALLOW)) {
        resultat = methode.invoke(o, parameterKonvertieren(methode, parameter, format));
        Class returnType = methode.getReturnType();
        /*
         * Wenn der Rueckgabewert der Methode nicht vom Typ String ist,
         * wird das Objekt in das Format verwandelt, das der Standardwandler
         * liefert, z.B. JSON
         */
        if(!returnType.getName().equals("java.lang.String")) {
          //resultat = new JsonWandler().vonJava(resultat, returnType);
          Wandler w = wandler.get(format);
          if(w != null) {
            resultat = w.vonJava(resultat, returnType);
          }
          /*if(format == Wandler.JSON) {
            resultat = new JsonWandler().vonJava(resultat, returnType);
          }*/
        }
        /*if(format == Wandler.JSON) {
                resultat = new JsonWandler().vonJava(resultat, returnType);
        }*/
            }
      } else {
        // nicht zum Zugriff vorgesehen
      }
        }
        return resultat;
    }