<commit_before>package org.apache.maven.archiva.web.action.admin.repositories;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.Preparable;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
import org.apache.maven.archiva.web.util.ContextUtils;

/**
 * RepositoryGroupsAction
 * 
 * @author
 * @version
 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="repositoryGroupsAction"
 */
public class RepositoryGroupsAction
    extends AbstractRepositoriesAdminAction
    implements ServletRequestAware, Preparable
{
    private RepositoryGroupConfiguration repositoryGroup;

    private Map<String, RepositoryGroupConfiguration> repositoryGroups;

    private Map<String, ManagedRepositoryConfiguration> managedRepositories;

    private Map<String, List<String>> groupToRepositoryMap;

    private String repoGroupId;
    
    private String repoId;

    /**
     * Used to construct the repository WebDAV URL in the repository action.
     */
    private String baseUrl;
    
    public void setServletRequest( HttpServletRequest request )
    {
        this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
    }

    public void prepare()
    {
        Configuration config = archivaConfiguration.getConfiguration();
        
        repositoryGroup = new RepositoryGroupConfiguration();
        repositoryGroups = config.getRepositoryGroupsAsMap();
        managedRepositories = config.getManagedRepositoriesAsMap();
        groupToRepositoryMap = config.getGroupToRepositoryMap();
    }
    
    public String addRepositoryGroup()
    {
        Configuration configuration = archivaConfiguration.getConfiguration();

        String repoGroupId = repositoryGroup.getId();
        
        if ( StringUtils.isBlank( repoGroupId ) )
        {
        	addActionError( "You must enter a repository group id." );
        	return ERROR;
        }
        
        if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a repository group." );
            return ERROR;
        }
        else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a managed repository." );
            return ERROR;
        }
        else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a remote repository." );
            return ERROR;
        }
        
        if( repoGroupId.length() > 100 )
        {
            addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
            return ERROR;
        }
            
        configuration.addRepositoryGroup( repositoryGroup );
        return saveConfiguration( configuration );
    }
    
    public String addRepositoryToGroup()
    {<commit_msg>[MRM-819] added validation to allow only alphanumeric, '.', '-' and '_' characters for repo group id<commit_after>package org.apache.maven.archiva.web.action.admin.repositories;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.webwork.interceptor.ServletRequestAware;
import com.opensymphony.xwork.Preparable;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.archiva.configuration.Configuration;
import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
import org.apache.maven.archiva.web.util.ContextUtils;

/**
 * RepositoryGroupsAction
 * 
 * @author
 * @version
 * @plexus.component role="com.opensymphony.xwork.Action" role-hint="repositoryGroupsAction"
 */
public class RepositoryGroupsAction
    extends AbstractRepositoriesAdminAction
    implements ServletRequestAware, Preparable
{
    private RepositoryGroupConfiguration repositoryGroup;

    private Map<String, RepositoryGroupConfiguration> repositoryGroups;

    private Map<String, ManagedRepositoryConfiguration> managedRepositories;

    private Map<String, List<String>> groupToRepositoryMap;

    private String repoGroupId;
    
    private String repoId;

    /**
     * Used to construct the repository WebDAV URL in the repository action.
     */
    private String baseUrl;
    
    private static final Pattern REPO_GROUP_ID_PATTERN = Pattern.compile( "[A-Za-z0-9\\._\\-]+" ); 
    
    public void setServletRequest( HttpServletRequest request )
    {
        this.baseUrl = ContextUtils.getBaseURL( request, "repository" );
    }

    public void prepare()
    {
        Configuration config = archivaConfiguration.getConfiguration();
        
        repositoryGroup = new RepositoryGroupConfiguration();
        repositoryGroups = config.getRepositoryGroupsAsMap();
        managedRepositories = config.getManagedRepositoriesAsMap();
        groupToRepositoryMap = config.getGroupToRepositoryMap();
    }
    
    public String addRepositoryGroup()
    {
        Configuration configuration = archivaConfiguration.getConfiguration();

        String repoGroupId = repositoryGroup.getId();
        
        if( repoGroupId == null || "".equals( repoGroupId.trim() ) )
        {
            addActionError( "Identifier field is required." );
            return ERROR;
        }
        
        if( repoGroupId.length() > 100 )
        {
            addActionError( "Identifier [" + repoGroupId + "] is over the maximum limit of 100 characters" );
            return ERROR;
        }
                
        Matcher matcher = REPO_GROUP_ID_PATTERN.matcher( repoGroupId );        
        if( !matcher.matches() )
        {
            addActionError( "Invalid character(s) found in identifier. Only the following characters are allowed: alphanumeric, '.', '-' and '_'" );
            return ERROR;
        }
        
        if ( StringUtils.isBlank( repoGroupId ) )
        {
        	addActionError( "You must enter a repository group id." );
        	return ERROR;
        }
        
        if ( configuration.getRepositoryGroupsAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a repository group." );
            return ERROR;
        }
        else if ( configuration.getManagedRepositoriesAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a managed repository." );
            return ERROR;
        }
        else if ( configuration.getRemoteRepositoriesAsMap().containsKey( repoGroupId ) )
        {
            addActionError( "Unable to add new repository group with id [" + repoGroupId
                    + "], that id already exists as a remote repository." );
            return ERROR;
        }
            
        configuration.addRepositoryGroup( repositoryGroup );
        return saveConfiguration( configuration );
    }
    
    public String addRepositoryToGroup()
    {
{'fraction_non_alphanumeric': 0.08005930318754632, 'fraction_numerical': 0.0026474637297469023, 'mean_word_length': 2.8880197612186085, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '4270347', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2308, 'n_words': 705}