// Path: src/main/java/org/commonwl/view/workflow/MultipleWorkflowsException.java
// @ResponseStatus(value = HttpStatus.MULTIPLE_CHOICES)
// public class MultipleWorkflowsException extends RuntimeException {
// 
//     private final Collection<Workflow> matches;
// 
//     public MultipleWorkflowsException(Workflow match) {
//         this(Collections.singleton(match));
//     }
// 
//     public MultipleWorkflowsException(Collection<Workflow> matches) {
//         if (matches.isEmpty()) {
//             throw new IllegalArgumentException("MultipleWorkflowsException, but empty list of workflows");
//         }
//         this.matches = matches;
//     }
// 
//     // Always CRLF in text/uri-list
//     private final String CRLF = "\r\n";
// 
//     public String getRawPermalink() {
//         // all raw URIs should be the same without ?part=
//         return matches.stream().findAny().get().getPermalink(Format.raw);
//     }
// 
//     /**
//      * Generate a text/uri-list of potential representations/redirects
//      * 
//      * @see https://www.iana.org/assignments/media-types/text/uri-list
//      */
//     @Override
//     public String toString() {
//         StringBuffer sb = new StringBuffer();
//         sb.append("## Multiple workflow representations found");
//         sb.append(CRLF);
//         sb.append("# ");
//         sb.append(CRLF);
// 
//         sb.append("# ");
//         sb.append(Format.raw.mediaType());
//         sb.append(CRLF);
//         sb.append(getRawPermalink());
//         sb.append(CRLF);
// 
//         Set<String> seen = new HashSet<>();
//         // For each workflow, link to each remaining format
//         for (Workflow w : matches) {
//             if (!seen.add(w.getIdentifier())) {
//                 // Skip permalink duplicates
//                 continue;
//             }
//             sb.append("#");
//             sb.append(CRLF);
//             sb.append("# ");
//             sb.append(w.getIdentifier());
//             sb.append(CRLF);
//             for (Format f : Format.values()) {
//                 if (f == Format.raw) {
//                     // Already did that one above
//                     continue;
//                 }
//                 sb.append("#   ");
//                 sb.append(f.mediaType());
//                 sb.append(CRLF);
//                 sb.append(w.getPermalink(f));
//                 sb.append(CRLF);
//             }
//         }
//         return sb.toString();
//     }
// 
// }
// 
// Path: src/main/java/org/commonwl/view/workflow/RepresentationNotFoundException.java
// @ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
// public class RepresentationNotFoundException extends RuntimeException {
// }
// 
// Path: src/main/java/org/commonwl/view/workflow/WorkflowNotFoundException.java
// @ResponseStatus(value = HttpStatus.NOT_FOUND)
// public class WorkflowNotFoundException extends RuntimeException {
// }


import java.util.Collections;
import org.commonwl.view.workflow.MultipleWorkflowsException;
import org.commonwl.view.workflow.RepresentationNotFoundException;
import org.commonwl.view.workflow.WorkflowNotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/*
 * 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.
 */

package org.commonwl.view;



/**
 * Handles exception handling across the application.
 * <p>
 * Because of Spring Boot's content negotiation these handlers are needed when
 * the error is returned with an otherwise "non acceptable" content type (e.g.
 * Accept: image/svg+xml but we have to say 404 Not Found)
 *
 */
@ControllerAdvice
public class GlobalControllerErrorHandling {

    /**
     * Workflow can not be found
     * @return A plain text error message
     */
    @ExceptionHandler(WorkflowNotFoundException.class)
    public ResponseEntity<?> handleNotFound() {
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<>("Workflow or git commit could not be found", headers, HttpStatus.NOT_FOUND);
    }

    /**
     * More than one workflow (or workflow parts) found
     *
     * @return A text/uri-list of potential representations
     */
// Path: src/main/java/org/commonwl/view/workflow/MultipleWorkflowsException.java
// @ResponseStatus(value = HttpStatus.MULTIPLE_CHOICES)
// public class MultipleWorkflowsException extends RuntimeException {
// 
//     private final Collection<Workflow> matches;
// 
//     public MultipleWorkflowsException(Workflow match) {
//         this(Collections.singleton(match));
//     }
// 
//     public MultipleWorkflowsException(Collection<Workflow> matches) {
//         if (matches.isEmpty()) {
//             throw new IllegalArgumentException("MultipleWorkflowsException, but empty list of workflows");
//         }
//         this.matches = matches;
//     }
// 
//     // Always CRLF in text/uri-list
//     private final String CRLF = "\r\n";
// 
//     public String getRawPermalink() {
//         // all raw URIs should be the same without ?part=
//         return matches.stream().findAny().get().getPermalink(Format.raw);
//     }
// 
//     /**
//      * Generate a text/uri-list of potential representations/redirects
//      * 
//      * @see https://www.iana.org/assignments/media-types/text/uri-list
//      */
//     @Override
//     public String toString() {
//         StringBuffer sb = new StringBuffer();
//         sb.append("## Multiple workflow representations found");
//         sb.append(CRLF);
//         sb.append("# ");
//         sb.append(CRLF);
// 
//         sb.append("# ");
//         sb.append(Format.raw.mediaType());
//         sb.append(CRLF);
//         sb.append(getRawPermalink());
//         sb.append(CRLF);
// 
//         Set<String> seen = new HashSet<>();
//         // For each workflow, link to each remaining format
//         for (Workflow w : matches) {
//             if (!seen.add(w.getIdentifier())) {
//                 // Skip permalink duplicates
//                 continue;
//             }
//             sb.append("#");
//             sb.append(CRLF);
//             sb.append("# ");
//             sb.append(w.getIdentifier());
//             sb.append(CRLF);
//             for (Format f : Format.values()) {
//                 if (f == Format.raw) {
//                     // Already did that one above
//                     continue;
//                 }
//                 sb.append("#   ");
//                 sb.append(f.mediaType());
//                 sb.append(CRLF);
//                 sb.append(w.getPermalink(f));
//                 sb.append(CRLF);
//             }
//         }
//         return sb.toString();
//     }
// 
// }
// 
// Path: src/main/java/org/commonwl/view/workflow/RepresentationNotFoundException.java
// @ResponseStatus(value = HttpStatus.NOT_ACCEPTABLE)
// public class RepresentationNotFoundException extends RuntimeException {
// }
// 
// Path: src/main/java/org/commonwl/view/workflow/WorkflowNotFoundException.java
// @ResponseStatus(value = HttpStatus.NOT_FOUND)
// public class WorkflowNotFoundException extends RuntimeException {
// }


// Path: src/main/java/org/commonwl/view/GlobalControllerErrorHandling.java
import java.util.Collections;
import org.commonwl.view.workflow.MultipleWorkflowsException;
import org.commonwl.view.workflow.RepresentationNotFoundException;
import org.commonwl.view.workflow.WorkflowNotFoundException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/*
 * 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.
 */

package org.commonwl.view;



/**
 * Handles exception handling across the application.
 * <p>
 * Because of Spring Boot's content negotiation these handlers are needed when
 * the error is returned with an otherwise "non acceptable" content type (e.g.
 * Accept: image/svg+xml but we have to say 404 Not Found)
 *
 */
@ControllerAdvice
public class GlobalControllerErrorHandling {

    /**
     * Workflow can not be found
     * @return A plain text error message
     */
    @ExceptionHandler(WorkflowNotFoundException.class)
    public ResponseEntity<?> handleNotFound() {
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<>("Workflow or git commit could not be found", headers, HttpStatus.NOT_FOUND);
    }

    /**
     * More than one workflow (or workflow parts) found
     *
     * @return A text/uri-list of potential representations
     */
    @ExceptionHandler(MultipleWorkflowsException.class)