<commit_before>/**
 * 
 */
package com.github.podd.resources;

import java.io.ByteArrayOutputStream;
import java.util.List;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Model;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.Rio;
import org.openrdf.rio.UnsupportedRDFormatException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.ByteArrayRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.security.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.ansell.restletutils.SesameRealmConstants;
import com.github.podd.restlet.PoddAction;
import com.github.podd.restlet.PoddSesameRealm;
import com.github.podd.restlet.PoddWebServiceApplication;
import com.github.podd.utils.PoddRdfConstants;
import com.github.podd.utils.PoddUser;
import com.github.podd.utils.PoddWebConstants;

/**
 * RDF Service to search Users.
 * 
 * @author kutila
 */
public class UserSearchResourceImpl extends AbstractPoddResourceImpl
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    
    /**
     * Search for PODD Users
     */
    @Get(":rdf|rj|json|ttl")
    public Representation searchUsersRdf(final Representation entity, final Variant variant) throws ResourceException
    {
        this.log.info("searchUsersRdf");
        
        final User user = this.getRequest().getClientInfo().getUser();
        this.log.info("authenticated user: {}", user);
        this.checkAuthentication(PoddAction.OTHER_USER_SEARCH);
        
        // - get input search term (mandatory)
        final String searchTerm = this.getQuery().getFirstValue(PoddWebConstants.KEY_SEARCHTERM);
        if(searchTerm == null)
        {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Search term not submitted");
        }
        
        // - search for matching users in Realm
        final PoddSesameRealm realm = ((PoddWebServiceApplication)this.getApplication()).getRealm();
        final List<PoddUser> resultList = realm.searchUser(searchTerm, null, false, -1, 0);
        
        // - convert results into a Model for sending back
        final Model resultModel = new LinkedHashModel();
        for(final PoddUser resultUser : resultList)
        {
            final String label =
                    resultUser.getFirstName() + " " + resultUser.getLastName() + ", " + resultUser.getOrganization();
            resultModel.add(resultUser.getUri(), RDFS.LABEL, PoddRdfConstants.VF.createLiteral(label));
            resultModel.add(resultUser.getUri(), SesameRealmConstants.OAS_USERIDENTIFIER,
                    PoddRdfConstants.VF.createLiteral(resultUser.getIdentifier()));
        }
        
        // - prepare response
        final ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
        final RDFFormat outputFormat =
                Rio.getWriterFormatForMIMEType(variant.getMediaType().getName(), RDFFormat.RDFXML);
        try
        {
            Rio.write(resultModel, output, outputFormat);<commit_msg>switch to use helper method PoddUser.getUserLabel()<commit_after>/**
 * 
 */
package com.github.podd.resources;

import java.io.ByteArrayOutputStream;
import java.util.List;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Model;
import org.openrdf.model.impl.LinkedHashModel;
import org.openrdf.model.vocabulary.RDFS;
import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.Rio;
import org.openrdf.rio.UnsupportedRDFormatException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.ByteArrayRepresentation;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.security.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.ansell.restletutils.SesameRealmConstants;
import com.github.podd.restlet.PoddAction;
import com.github.podd.restlet.PoddSesameRealm;
import com.github.podd.restlet.PoddWebServiceApplication;
import com.github.podd.utils.PoddRdfConstants;
import com.github.podd.utils.PoddUser;
import com.github.podd.utils.PoddWebConstants;

/**
 * RDF Service to search Users.
 * 
 * @author kutila
 */
public class UserSearchResourceImpl extends AbstractPoddResourceImpl
{
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    
    /**
     * Search for PODD Users
     */
    @Get(":rdf|rj|json|ttl")
    public Representation searchUsersRdf(final Representation entity, final Variant variant) throws ResourceException
    {
        this.log.info("searchUsersRdf");
        
        final User user = this.getRequest().getClientInfo().getUser();
        this.log.info("authenticated user: {}", user);
        this.checkAuthentication(PoddAction.OTHER_USER_SEARCH);
        
        // - get input search term (mandatory)
        final String searchTerm = this.getQuery().getFirstValue(PoddWebConstants.KEY_SEARCHTERM);
        if(searchTerm == null)
        {
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Search term not submitted");
        }
        
        // - search for matching users in Realm
        final PoddSesameRealm realm = ((PoddWebServiceApplication)this.getApplication()).getRealm();
        final List<PoddUser> resultList = realm.searchUser(searchTerm, null, false, -1, 0);
        
        // - convert results into a Model for sending back
        final Model resultModel = new LinkedHashModel();
        for(final PoddUser resultUser : resultList)
        {
            final String label = resultUser.getUserLabel();
            resultModel.add(resultUser.getUri(), RDFS.LABEL, PoddRdfConstants.VF.createLiteral(label));
            resultModel.add(resultUser.getUri(), SesameRealmConstants.OAS_USERIDENTIFIER,
                    PoddRdfConstants.VF.createLiteral(resultUser.getIdentifier()));
        }
        
        // - prepare response
        final ByteArrayOutputStream output = new ByteArrayOutputStream(8096);
        final RDFFormat outputFormat =
                Rio.getWriterFormatForMIMEType(variant.getMediaType().getName(), RDFFormat.RDFXML);
        try
        {
            Rio.write(resultModel, output, outputFormat);
{'fraction_non_alphanumeric': 0.10074570080657434, 'fraction_numerical': 0.0024349414092223404, 'mean_word_length': 3.473791695030633, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '985535', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1926, 'n_words': 356}