<commit_before>/**
 * A RESTful web service on top of DSpace.
 * Copyright (C) 2010-2011 National Library of Finland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package fi.helsinki.lib.simplerest;

import com.google.gson.Gson;
import fi.helsinki.lib.simplerest.stubs.StubUser;
import java.sql.SQLException;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class UserResource extends BaseResource {

    private static Logger log = Logger.getLogger(UserResource.class);

    private int userId;
    private EPerson eperson;
    private Context context;
    
    public UserResource(EPerson p, int userId){
        this.eperson = p;
        this.userId = userId;
    }
    
    public UserResource(){
        this.userId = 0;
        this.eperson = null;
        try{
            this.context = new Context();
        }catch(SQLException e){
            log.log(Priority.FATAL, e);
        }
    }

    @Override
    protected void doInit() throws ResourceException {
        try {
            String s = (String)getRequest().getAttributes().get("userId");
            this.userId = Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            ResourceException resourceException =
                new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                                      "User ID must be a number.");
            throw resourceException;
        }
    }

    static public String relativeUrl(int userId) {
        return "user/" + userId;
    }

    @Get("html|xhtml|xml")
    public Representation toXml() {
        DomRepresentation representation;
        Document d = null;
        
        try{
            this.eperson = EPerson.find(this.context, this.userId);
        }catch(Exception e){
            if(this.eperson == null){
                return errorInternal(this.context, e.toString());
            }
            log.log(Priority.INFO, e);
        }
	
        try {
            representation = new DomRepresentation(MediaType.TEXT_HTML);  
            d = representation.getDocument();  
        }
        catch (Exception e) {
            return errorInternal(this.context, e.toString());
        }<commit_msg>should 404 if person is not found<commit_after>/**
 * A RESTful web service on top of DSpace.
 * Copyright (C) 2010-2011 National Library of Finland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package fi.helsinki.lib.simplerest;

import com.google.gson.Gson;
import fi.helsinki.lib.simplerest.stubs.StubUser;
import java.sql.SQLException;
import org.dspace.core.Context;
import org.dspace.eperson.EPerson;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.ResourceException;
import org.restlet.data.MediaType;
import org.restlet.data.Status;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

public class UserResource extends BaseResource {

    private static Logger log = Logger.getLogger(UserResource.class);

    private int userId;
    private EPerson eperson;
    private Context context;
    
    public UserResource(EPerson p, int userId){
        this.eperson = p;
        this.userId = userId;
    }
    
    public UserResource(){
        this.userId = 0;
        this.eperson = null;
        try{
            this.context = new Context();
        }catch(SQLException e){
            log.log(Priority.FATAL, e);
        }
    }

    @Override
    protected void doInit() throws ResourceException {
        try {
            String s = (String)getRequest().getAttributes().get("userId");
            this.userId = Integer.parseInt(s);
        }
        catch (NumberFormatException e) {
            ResourceException resourceException =
                new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
                                      "User ID must be a number.");
            throw resourceException;
        }
    }

    static public String relativeUrl(int userId) {
        return "user/" + userId;
    }

    @Get("html|xhtml|xml")
    public Representation toXml() {
        DomRepresentation representation;
        Document d = null;
        
        try{
            this.eperson = EPerson.find(this.context, this.userId);
        }catch(Exception e){
            if(this.eperson == null){
                return errorNotFound(this.context, "Could not find the community");
            }
            log.log(Priority.INFO, e);
        }
	
        try {
            representation = new DomRepresentation(MediaType.TEXT_HTML);  
            d = representation.getDocument();  
        }
        catch (Exception e) {
            return errorInternal(this.context, e.toString());
        }
{'fraction_non_alphanumeric': 0.08688374923919659, 'fraction_numerical': 0.008368837492391966, 'mean_word_length': 2.53767491926803, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '371501', 'n_tokens_mistral': 1803, 'n_tokens_neox': 1756, 'n_words': 560}