com.yoshio3.azuread.graph.GraphAPIImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.yoshio3.azuread.graph.GraphAPIImpl.java

Source

/*
* Copyright 2016 Yoshio Terada
*
* 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.
 */
package com.yoshio3.azuread.graph;

import com.yoshio3.azuread.entities.ADUsers;
import com.yoshio3.azuread.entities.ADUser;
import com.yoshio3.azuread.entities.ADGroups;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import com.yoshio3.azuread.entities.ADGroup;
import com.yoshio3.azuread.entities.ADUserMemberOfGroups;
import com.yoshio3.jaspic.AzureADUserPrincipal;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.security.PermitAll;
import javax.enterprise.context.Dependent;
import javax.faces.context.FacesContext;
import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonWriter;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;

/*
 ?
 ???????
 Reference information
    
 Graph Explorer ????
 Graph Explorer ?????????
 Graph Explorer Convenient tool accessible from the browser
 https://graphexplorer.cloudapp.net/    //??
                                    //??????
                                    //Current one
 https://graphexplorer2.azurewebsites.net/  // ????
                                        // ?????????????
                                        // New one: not supported yet
    
 Graph API ?
 Graph API ??
 Graph API query examples
    
 https://msdn.microsoft.com/Library/Azure/Ad/Graph/howto/azure-ad-graph-api-supported-queries-filters-and-paging-options#CommonQueries
 https://graph.microsoft.io/ja-jp/docs/authorization/app_authorization
    
  ???
  ????????
  Modified based on this
    
  https://azure.microsoft.com/ja-jp/documentation/articles/active-directory-devquickstarts-webapp-java/
 */
/**
 *
 * @author Yoshio Terada
 */
@Dependent
@PermitAll
public class GraphAPIImpl implements Serializable {

    private static final String PRINCIPAL_SESSION_NAME = "principal";
    private String tenant;
    private String authString;
    private Client jaxrsClient;
    private final static String GRAPH_SEVER = "graph.windows.net";
    private final static Logger LOGGER = Logger.getLogger(GraphAPIImpl.class.getName());

    @PostConstruct
    public void init() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext()
                .getRequest();
        init(request);
    }

    public void init(HttpServletRequest request) {
        AzureADUserPrincipal userPrincipal = (AzureADUserPrincipal) request.getSession()
                .getAttribute(PRINCIPAL_SESSION_NAME);

        authString = "Bearer " + userPrincipal.getAuthenticationResult().getAccessToken();
        tenant = request.getServletContext().getInitParameter("tenant");

        jaxrsClient = ClientBuilder.newClient().register(
                (new JacksonJaxbJsonProvider(new ObjectMapper(), JacksonJaxbJsonProvider.DEFAULT_ANNOTATIONS)))
                .register(JacksonFeature.class);
        System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
    }

    /* ??? */
    /* Getting all registered users */
    public ADUsers getAllADUserFromGraph() {
        String graphURL = String.format("https://%s/%s/users", GRAPH_SEVER, tenant);

        //Response res = jaxrsClient.target(graphURL)
        ADUsers users = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("api-version", "1.6")
                .header("Authorization", authString)
                //.get();
                .get(ADUsers.class);
        //LOGGER.log(Level.INFO, res.toString());
        LOGGER.log(Level.INFO, users.toString());
        //return new ADUsers();
        return users;
    }

    /* ?? ID ()?? AD?? */
    /* Getting an AD user for a specified ID (mail address) */
    public ADUser getADUserFromGraph(String id) {
        String graphURL = String.format("https://%s/%s/users/%s", GRAPH_SEVER, tenant, id);
        ADUser user = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("api-version", "1.6")
                .header("Authorization", authString).get(ADUser.class);
        LOGGER.log(Level.INFO, user.toString());
        return user;
    }

    public ADGroups getAllADGroupFromGraph() {
        String graphURL = String.format("https://%s/%s/groups", GRAPH_SEVER, tenant);

        ADGroups groups = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("api-version", "1.6")
                .header("Authorization", authString).get(ADGroups.class);
        LOGGER.log(Level.INFO, groups.toString());
        return groups;
    }

    /* ?? ID ??? */
    /* Getting an AD group for a specified group ID */
    public ADGroup getADGroupFromGraph(String groupid) {
        String graphURL = String.format("https://%s/%s/groups/%s", GRAPH_SEVER, tenant, groupid);

        ADGroup group = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("api-version", "1.6")
                .header("Authorization", authString).get(ADGroup.class);
        LOGGER.log(Level.INFO, group.toString());
        return group;
    }

    /* ?? ID ??? */
    /* Getting the list of users that belong to a specified group ID */
    public ADUsers getAllUsersInGroup(String groupid) {
        String graphURL = String.format("https://%s/%s/groups/%s/members", GRAPH_SEVER, tenant, groupid);
        ADUsers users = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("api-version", "1.6")
                .header("Authorization", authString).get(ADUsers.class);
        LOGGER.log(Level.INFO, users.toString());
        return users;
    }

    /* ?? ID ???? */
    /* Getting the list of group that a specified user ID belongs to */
    public ADUserMemberOfGroups getMemberOfGroup(String userID) {
        String graphURL = String.format("https://%s/%s/users/%s/getMemberGroups", GRAPH_SEVER, tenant, userID);
        JsonObject model = Json.createObjectBuilder().add("securityEnabledOnly", "false").build();
        StringWriter stWriter = new StringWriter();
        try (JsonWriter jsonWriter = Json.createWriter(stWriter)) {
            jsonWriter.writeObject(model);
        }
        String jsonData = stWriter.toString();

        Response response = jaxrsClient.target(graphURL).request().header("Host", GRAPH_SEVER)
                .header("Accept", "application/json, text/plain, */*").header("Content-Type", "application/json")
                .header("api-version", "1.6").header("Authorization", authString).post(Entity.json(jsonData));
        ADUserMemberOfGroups memberOfGrups = response.readEntity(ADUserMemberOfGroups.class);
        LOGGER.log(Level.INFO, memberOfGrups.toString());
        return memberOfGrups;
    }
}