<commit_before>/*******************************************************************************
 * Copyright (c) 2015 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.rest.v1.resource.image;

import com.whizzosoftware.hobson.api.image.ImageInputStream;
import com.whizzosoftware.hobson.api.image.ImageManager;
import com.whizzosoftware.hobson.rest.v1.HobsonRestContext;
import com.whizzosoftware.hobson.rest.v1.JSONMarshaller;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.guice.SelfInjectingServerResource;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;

import javax.inject.Inject;
import java.io.ByteArrayInputStream;

/**
 * A REST resource for setting/retrieving a hub image.
 *
 * @author <NAME>
 */
public class HubImageResource extends SelfInjectingServerResource {
    public static final String REL = "image";
    public static final String PATH = "/users/{userId}/hubs/{hubId}/image";

    @Inject
    ImageManager imageManager;

    /**
     * @api {get} /api/v1/users/:userId/hubs/:hubId/image Get Hub image
     * @apiVersion 0.5.0
     * @apiName GetHubImage
     * @apiDescription Retrieves the binary image data associated with the hub.
     * @apiGroup Hub
     * @apiSuccessExample Success Response:
     *   HTTP/1.1 200 OK
     *   Content-Type: image/jpeg
     *   ...
     */
    @Override
    protected Representation get() throws ResourceException {
        ImageInputStream iis = imageManager.getHubImage();
        return new InputRepresentation(iis.getInputStream(), MediaType.valueOf(iis.getMediaType()));
    }

    /**
     * @api {put} /api/v1/users/:userId/hubs/:hubId/image Set Hub image
     * @apiVersion 0.5.0
     * @apiName SetHubImage
     * @apiDescription Sets the image associated with the hub. The PUT request should specify a Content-Type header and can be either a JSON image library reference (application/json) or raw binary data (image/jpeg or image/png).
     * @apiGroup Hub
     * @apiParamExample {json} Example Request (Image library reference):
     * {
     *   "imageLibRef": "/api/v1/users/local/hubs/local/imageLibrary/images/light7.png"
     * }
     * @apiParamExample {json} Example Request (Image data):
     * {
     *   "image": {
     *     "mediaType": "image/jpeg",
     *     "data": "Base64-encoded image data string"
     *   }
     * }
     * @apiSuccessExample Success Response:
     *   HTTP/1.1 202 Accepted
     */
    @Override
    protected Representation put(Representation entity) throws ResourceException {
        HobsonRestContext ctx = HobsonRestContext.createContext(this, getRequest());
        JSONObject json = JSONMarshaller.createJSONFromRepresentation(entity);<commit_msg>Updated API description.<commit_after>/*******************************************************************************
 * Copyright (c) 2015 Whizzo Software, LLC.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package com.whizzosoftware.hobson.rest.v1.resource.image;

import com.whizzosoftware.hobson.api.image.ImageInputStream;
import com.whizzosoftware.hobson.api.image.ImageManager;
import com.whizzosoftware.hobson.rest.v1.HobsonRestContext;
import com.whizzosoftware.hobson.rest.v1.JSONMarshaller;
import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.ext.guice.SelfInjectingServerResource;
import org.restlet.representation.EmptyRepresentation;
import org.restlet.representation.InputRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.ResourceException;

import javax.inject.Inject;
import java.io.ByteArrayInputStream;

/**
 * A REST resource for setting/retrieving a hub image.
 *
 * @author <NAME>
 */
public class HubImageResource extends SelfInjectingServerResource {
    public static final String REL = "image";
    public static final String PATH = "/users/{userId}/hubs/{hubId}/image";

    @Inject
    ImageManager imageManager;

    /**
     * @api {get} /api/v1/users/:userId/hubs/:hubId/image Get Hub image
     * @apiVersion 0.5.0
     * @apiName GetHubImage
     * @apiDescription Retrieves the binary image data associated with the hub.
     * @apiGroup Hub
     * @apiSuccessExample Success Response:
     *   HTTP/1.1 200 OK
     *   Content-Type: image/jpeg
     *   ...
     */
    @Override
    protected Representation get() throws ResourceException {
        ImageInputStream iis = imageManager.getHubImage();
        return new InputRepresentation(iis.getInputStream(), MediaType.valueOf(iis.getMediaType()));
    }

    /**
     * @api {put} /api/v1/users/:userId/hubs/:hubId/image Set Hub image
     * @apiVersion 0.5.0
     * @apiName SetHubImage
     * @apiDescription Sets the image associated with the hub. The request JSON can specify an "imageLibRef" property or an "image" property as shown in the examples below.
     * @apiGroup Hub
     * @apiParamExample {json} Example Request (Image library reference):
     * {
     *   "imageLibRef": "/api/v1/users/local/hubs/local/imageLibrary/images/light7.png"
     * }
     * @apiParamExample {json} Example Request (Image data):
     * {
     *   "image": {
     *     "mediaType": "image/jpeg",
     *     "data": "Base64-encoded image data string"
     *   }
     * }
     * @apiSuccessExample Success Response:
     *   HTTP/1.1 202 Accepted
     */
    @Override
    protected Representation put(Representation entity) throws ResourceException {
        HobsonRestContext ctx = HobsonRestContext.createContext(this, getRequest());
        JSONObject json = JSONMarshaller.createJSONFromRepresentation(entity);
{'fraction_non_alphanumeric': 0.14674247080516287, 'fraction_numerical': 0.0107559926244622, 'mean_word_length': 3.789551140544518, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4048731', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1833, 'n_words': 454}