<commit_before>package org.curriki.xwiki.servlet.restlet.resource.assets;

import org.restlet.resource.Representation;
import org.restlet.resource.Variant;
import org.restlet.resource.ResourceException;
import org.restlet.Context;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.curriki.xwiki.servlet.restlet.resource.BaseResource;
import org.curriki.xwiki.plugin.asset.Asset;
import org.curriki.xwiki.plugin.asset.DefaultAssetManager;
import org.curriki.xwiki.plugin.asset.Constants;
import org.curriki.xwiki.plugin.asset.external.ExternalAsset;
import org.curriki.xwiki.plugin.asset.external.ExternalAssetManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONException;
import com.xpn.xwiki.XWikiException;

/**
 */
public class ExternalsResource extends BaseResource {
    public ExternalsResource(Context context, Request request, Response response) {
        super(context, request, response);
        setReadable(true);
        setModifiable(true);
        defaultVariants();
    }

    @Override public Representation represent(Variant variant) throws ResourceException {
        setupXWiki();

        Request request = getRequest();
        String assetName = (String) request.getAttributes().get("assetName");

        ExternalAsset asset = null;
        try {
            asset = (ExternalAsset) plugin.fetchAssetAs(assetName, ExternalAsset.class);
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage());
        }
        if (asset == null) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "External Asset "+assetName+" not found.");
        }

        String link = null;
        try {
            link = asset.getLink();
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "No links found for "+assetName);
        }

        String linktext = null;
        try {
            linktext = asset.getLinkText();
        } catch (XWikiException e) {
            linktext = "";
        }

        JSONArray jsonArray = new JSONArray();

        JSONObject json = new JSONObject();
        json.put("href", getChildReference(getRequest().getResourceRef(), "0"));
        json.put("link", link);
        json.put("linktext", linktext);

        jsonArray.add(json);

        return formatJSON(jsonArray, variant);
    }

    @Override public void acceptRepresentation(Representation representation) throws ResourceException {
        setupXWiki();

        Request request = getRequest();
        String assetName = (String) request.getAttributes().get("assetName");

        JSONObject json = representationToJSONObject(representation);
        String link;
        try {
            link = json.getString("link");
        } catch (JSONException e) {
            throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "You must provide a link.");
        }

        String linktext;
        try {
            linktext = json.getString("linktext");
        } catch (JSONException e) {
            linktext = "";
        }

        Asset asset;
        try {
            asset = plugin.fetchAssetSubclassAs(assetName, ExternalAsset.class);
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage());
        }
        if (asset == null) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "External Asset "+assetName+" not found.");
        }

        try {
            // call the sub type asset manager to get more details
            ExternalAssetManager assetManager = (ExternalAssetManager) DefaultAssetManager.getAssetSubTypeManager(Constants.ASSET_CATEGORY_EXTERNAL);
            if (assetManager!=null) {
                assetManager.makeExternalAsset(asset, link, (linktext==null) ? "" : linktext);
            } else {
                throw error(Status.CLIENT_ERROR_PRECONDITION_FAILED, "Could not add external link");
            }
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_PRECONDITION_FAILED, "Could not add external link");
        }<commit_msg>SKE-325 Fixed issue with link too long. more than 255 chars<commit_after>package org.curriki.xwiki.servlet.restlet.resource.assets;

import org.restlet.resource.Representation;
import org.restlet.resource.Variant;
import org.restlet.resource.ResourceException;
import org.restlet.Context;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.curriki.xwiki.servlet.restlet.resource.BaseResource;
import org.curriki.xwiki.plugin.asset.Asset;
import org.curriki.xwiki.plugin.asset.DefaultAssetManager;
import org.curriki.xwiki.plugin.asset.Constants;
import org.curriki.xwiki.plugin.asset.external.ExternalAsset;
import org.curriki.xwiki.plugin.asset.external.ExternalAssetManager;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONException;
import com.xpn.xwiki.XWikiException;

/**
 */
public class ExternalsResource extends BaseResource {
    public ExternalsResource(Context context, Request request, Response response) {
        super(context, request, response);
        setReadable(true);
        setModifiable(true);
        defaultVariants();
    }

    @Override public Representation represent(Variant variant) throws ResourceException {
        setupXWiki();

        Request request = getRequest();
        String assetName = (String) request.getAttributes().get("assetName");

        ExternalAsset asset = null;
        try {
            asset = (ExternalAsset) plugin.fetchAssetAs(assetName, ExternalAsset.class);
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage());
        }
        if (asset == null) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "External Asset "+assetName+" not found.");
        }

        String link = null;
        try {
            link = asset.getLink();
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "No links found for "+assetName);
        }

        String linktext = null;
        try {
            linktext = asset.getLinkText();
        } catch (XWikiException e) {
            linktext = "";
        }

        JSONArray jsonArray = new JSONArray();

        JSONObject json = new JSONObject();
        json.put("href", getChildReference(getRequest().getResourceRef(), "0"));
        json.put("link", link);
        json.put("linktext", linktext);

        jsonArray.add(json);

        return formatJSON(jsonArray, variant);
    }

    @Override public void acceptRepresentation(Representation representation) throws ResourceException {
        setupXWiki();

        Request request = getRequest();
        String assetName = (String) request.getAttributes().get("assetName");

        JSONObject json = representationToJSONObject(representation);
        String link;
        try {
            link = json.getString("link");
        } catch (JSONException e) {
            throw error(Status.CLIENT_ERROR_NOT_ACCEPTABLE, "You must provide a link.");
        }

        String linktext;
        try {
            linktext = json.getString("linktext");
        } catch (JSONException e) {
            linktext = "";
        }

        Asset asset;
        try {
            asset = plugin.fetchAssetSubclassAs(assetName, ExternalAsset.class);
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, e.getMessage());
        }
        if (asset == null) {
            throw error(Status.CLIENT_ERROR_NOT_FOUND, "External Asset "+assetName+" not found.");
        }

        try {
            // call the sub type asset manager to get more details
            ExternalAssetManager assetManager = (ExternalAssetManager) DefaultAssetManager.getAssetSubTypeManager(Constants.ASSET_CATEGORY_EXTERNAL);
            if (assetManager!=null) {
                if (link.length() > 255)
                    link = link.split("#")[0];
                assetManager.makeExternalAsset(asset, link, (linktext==null) ? "" : linktext);
            } else {
                throw error(Status.CLIENT_ERROR_PRECONDITION_FAILED, "Could not add external link");
            }
        } catch (XWikiException e) {
            throw error(Status.CLIENT_ERROR_PRECONDITION_FAILED, "Could not add external link");
        }
{'fraction_non_alphanumeric': 0.1037914691943128, 'fraction_numerical': 0.0014218009478672985, 'mean_word_length': 2.67, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '5665448', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2280, 'n_words': 497}