Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import org.apache.servicecomb.common.rest.codec.RestClientRequest;
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.common.rest.codec.param.HeaderProcessorCreator.HeaderProcessor;

import jakarta.servlet.http.HttpServletRequest;

public interface HeaderCodec {
static String encodeValue(Object value) throws UnsupportedEncodingException {
return URLEncoder.encode(value.toString(), StandardCharsets.UTF_8.name());
}

// can not be replaced by value.toString() because of date serialize
static String convertToString(Object value) throws Exception {
return RestObjectMapperFactory.getRestObjectMapper().convertToString(value);
}

String getCodecName();

void encode(RestClientRequest clientRequest, String name, Object value) throws Exception;

Object decode(HeaderProcessor processor, HttpServletRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

public class HeaderCodecCsv extends HeaderCodecWithDelimiter {
public static final String CODEC_NAME = "form:0";

public static final String DELIMITER = ",";

public HeaderCodecCsv() {
super(CODEC_NAME, DELIMITER, DELIMITER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;

import org.apache.servicecomb.common.rest.codec.RestClientRequest;
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.common.rest.codec.param.HeaderProcessorCreator.HeaderProcessor;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.core.Response.Status;

public class HeaderCodecMulti implements HeaderCodec {
public static final String NAME = "form:1";

@Override
public String getCodecName() {
return NAME;
}

@Override
public void encode(RestClientRequest clientRequest, String name, Object value) throws Exception {
if (null == value) {
// if value is empty, header should not be set to clientRequest to avoid NullPointerException in Netty.
return;
}
if (!(value instanceof Collection<?>)) {
throw new InvocationException(Status.BAD_REQUEST,
new CommonExceptionData("Array type of header should be Collection"));
}
for (Object item : ((Collection<?>) value)) {
clientRequest.putHeader(name,
RestObjectMapperFactory.getConsumerWriterMapper().convertToString(item));
}
}

@Override
public Object decode(HeaderProcessor processor, HttpServletRequest request) {
Enumeration<String> headerValues = request.getHeaders(processor.getParameterPath());
if (headerValues == null) {
//Even if the paramPath does not exist, headerValues won't be null at now
return null;
}
return processor.convertValue(Collections.list(headerValues), processor.getTargetType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

public class HeaderCodecPipes extends HeaderCodecWithDelimiter {
public static final String CODEC_NAME = "pipeDelimited:0";

public static final String JOIN_DELIMITER = "|";

public static final String SPLIT_DELIMITER = "\\|";

public HeaderCodecPipes() {
super(CODEC_NAME, JOIN_DELIMITER, SPLIT_DELIMITER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

import org.apache.servicecomb.common.rest.codec.RestClientRequest;
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.common.rest.codec.param.HeaderProcessorCreator.HeaderProcessor;

import jakarta.servlet.http.HttpServletRequest;

public class HeaderCodecSimple implements HeaderCodec {
public static final String NAME = "simple";

@Override
public String getCodecName() {
return NAME;
}

@Override
public void encode(RestClientRequest clientRequest, String name, Object value) throws Exception {
if (null == value) {
// if value is empty, header should not be set to clientRequest to avoid NullPointerException in Netty.
return;
}
clientRequest.putHeader(name,
RestObjectMapperFactory.getConsumerWriterMapper().convertToString(value));
}

@Override
public Object decode(HeaderProcessor processor, HttpServletRequest request) {
Object value = request.getHeader(processor.getParameterPath());
if (value == null) {
value = processor.checkRequiredAndDefaultValue();
}
return processor.convertValue(value, processor.getTargetType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

public class HeaderCodecSsv extends HeaderCodecWithDelimiter {
public static final String CODEC_NAME = "spaceDelimited:0";

public static final String DELIMITER = " ";

public HeaderCodecSsv() {
super(CODEC_NAME, DELIMITER, DELIMITER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

import java.util.Collection;
import java.util.List;
import java.util.StringJoiner;

import org.apache.servicecomb.common.rest.codec.RestClientRequest;
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.common.rest.codec.param.HeaderProcessorCreator.HeaderProcessor;
import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.ws.rs.core.Response.Status;

public abstract class HeaderCodecWithDelimiter implements HeaderCodec {
private final String name;

private final String joinDelimiter;

private final String splitDelimiter;

public HeaderCodecWithDelimiter(String name, String joinDelimiter, String splitDelimiter) {
this.name = name;
this.joinDelimiter = joinDelimiter;
this.splitDelimiter = splitDelimiter;
}


@Override
public String getCodecName() {
return name;
}

@Override
public void encode(RestClientRequest clientRequest, String name, Object value) throws Exception {
if (null == value) {
// if value is empty, header should not be set to clientRequest to avoid NullPointerException in Netty.
return;
}
if (!(value instanceof Collection<?>)) {
throw new InvocationException(Status.BAD_REQUEST,
new CommonExceptionData("Array type of header should be Collection"));
}
clientRequest.putHeader(name, join((Collection<?>) value));
}

protected String join(Collection<?> values) throws Exception {
StringJoiner joiner = new StringJoiner(joinDelimiter);
for (Object value : values) {
String strValue = RestObjectMapperFactory.getConsumerWriterMapper().convertToString(value);
joiner.add(strValue);
}

return joiner.toString();
}

@Override
public Object decode(HeaderProcessor processor, HttpServletRequest request) {
String headerValues = request.getHeader(processor.getParameterPath());
if (headerValues == null) {
headerValues = (String) processor.checkRequiredAndDefaultValue();
}

return processor.convertValue(List.of(headerValues.split(splitDelimiter)), processor.getTargetType());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.apache.servicecomb.common.rest.codec.header;

import java.util.HashMap;
import java.util.Map;

import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.parameters.Parameter.StyleEnum;

public class HeaderCodecsUtils {
private static final Map<String, HeaderCodec> CODECS;

static {
CODECS = new HashMap<>();
CODECS.put(HeaderCodecSimple.NAME, new HeaderCodecSimple());
CODECS.put(HeaderCodecMulti.NAME, new HeaderCodecMulti());
CODECS.put(HeaderCodecCsv.CODEC_NAME, new HeaderCodecCsv());
CODECS.put(HeaderCodecPipes.CODEC_NAME, new HeaderCodecPipes());
CODECS.put(HeaderCodecSsv.CODEC_NAME, new HeaderCodecSsv());
}

private HeaderCodecsUtils() {
}

public static HeaderCodec find(Parameter.StyleEnum styleEnum, Boolean explode) {
return CODECS.get(formatName(styleEnum, explode));
}

private static String formatName(StyleEnum styleEnum, Boolean explode) {
if (styleEnum == null) {
return HeaderCodecSimple.NAME;
}
return styleEnum + ":" +
(explode != null && explode ? "1" : "0");
}
}
Loading